本文整理汇总了C#中FormContext.GetJsonValidationMetadata方法的典型用法代码示例。如果您正苦于以下问题:C# FormContext.GetJsonValidationMetadata方法的具体用法?C# FormContext.GetJsonValidationMetadata怎么用?C# FormContext.GetJsonValidationMetadata使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FormContext
的用法示例。
在下文中一共展示了FormContext.GetJsonValidationMetadata方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetJsonValidationMetadata_ValidationSummary
public void GetJsonValidationMetadata_ValidationSummary()
{
// Arrange
FormContext context = new FormContext() { FormId = "theFormId", ValidationSummaryId = "validationSummary" };
ModelClientValidationRule rule = new ModelClientValidationRule() { ValidationType = "ValidationType1", ErrorMessage = "Error Message" };
rule.ValidationParameters["theParam"] = new { FirstName = "John", LastName = "Doe", Age = 32 };
FieldValidationMetadata metadata = new FieldValidationMetadata() { FieldName = "theFieldName", ValidationMessageId = "theFieldName_ValidationMessage" };
metadata.ValidationRules.Add(rule);
context.FieldValidators["theFieldName"] = metadata;
// Act
string jsonMetadata = context.GetJsonValidationMetadata();
// Assert
string expected = @"{""Fields"":[{""FieldName"":""theFieldName"",""ReplaceValidationMessageContents"":false,""ValidationMessageId"":""theFieldName_ValidationMessage"",""ValidationRules"":[{""ErrorMessage"":""Error Message"",""ValidationParameters"":{""theParam"":{""FirstName"":""John"",""LastName"":""Doe"",""Age"":32}},""ValidationType"":""ValidationType1""}]}],""FormId"":""theFormId"",""ReplaceValidationSummary"":false,""ValidationSummaryId"":""validationSummary""}";
Assert.Equal(expected, jsonMetadata);
}
示例2: OutputClientValidation
private static void OutputClientValidation(HttpResponseBase response, FormContext formContext) {
if (!formContext.ClientValidationEnabled) {
return; // do nothing
}
// output a call that resembles:
// _clientValidationFunction(validationObject, userContext);
string validationJson = formContext.GetJsonValidationMetadata();
string userContextJson = (formContext.ClientValidationState != null)
? new JavaScriptSerializer().Serialize(formContext.ClientValidationState)
: "null";
string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);
string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines,
formContext.ClientValidationFunction, validationJson, userContextJson);
response.Write(formatted);
}