Soo*_*ead 13 c# data-annotations asp.net-mvc-3
这更像是一个理论问题.
我目前正在使用ComponentModel.DataAnnotations检查MVC 3验证,一切都在自动运行,特别是在客户端.
某种程度上会检查这些属性,并为验证生成javascript(或html5属性,如果使用不显眼的模式),它可以工作.
我的问题是什么生成客户端javascript以及如何访问和修改它?例如,我想稍微处理给定的dataannotation属性,或处理自定义属性(我发现我可以从ValidationAttribute派生它们,但可能由于某些原因我不想要).
有人可以向我解释一下真正发生了什么吗?(或者链接到好的解释也会很好,因为我只找到了实际使用数据注释的教程)
编辑:此外,从ValidationAttribute派生,客户端验证不会自动运行.为什么?
bal*_*dre 15
MVC3有一个新的jQuery验证机制,它链接jQuery验证和验证属性元数据,这是一个jquery.validate.unobtrusive获取所有data-属性并使用它们的文件,就像之前设置时一样
<add key="UnobtrusiveJavaScriptEnabled" value="false" />
Run Code Online (Sandbox Code Playgroud)
您需要做的就是提出自己的自定义验证属性,因为您有2个选项:
ValidationAttributeIsValid 要么
IValidatebleObject您需要的模型来返回Validate方法在MVC3中,您现在有一个方法可以覆盖具有ValidationContext对象的方法,您可以在其中简单地获取表单中任何其他对象的所有引用,属性和值
创建您自己的,这个不显眼的文件将处理您的自定义验证器需要的映射,并将与jQuery Validation插件一起使用.
你不要改变javascript ...那是sooo 90's而不是MVC方式!
例如,如果你想验证,让我们说2个日期,最后一个不能小于第一个(例如时间段)
public class TimeCard
{
public DateTime StartDate { get; set; }
[GreaterThanDateAttribute("StartDate")]
public DateTime EndDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
创建自定义验证
public class GreaterThanDateAttribute : ValidationAttribute
{
public string GreaterThanDateAttribute(string otherPropertyName)
:base("{0} must be greater than {1}")
{
OtherPropertyName = otherPropertyName;
}
public override string FormatErrorMessage(string name)
{
return String.Format(ErrorMessageString, name, OtherPropertyName);
}
public override ValidateionResult IsValid(object value, ValidationContext validationContext)
{
var otherPropertyInfo = validationContext.ObjectTYpe.GetProperty(OtherPropertyName);
var otherDate = (DateTime)otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
var thisDate = (DateTime)value;
if( thisDate <= otherDate )
{
var message = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(message);
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果使用自我验证模型,那么代码就是
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if( EndDate <= StartDate )
yield return new ValidationResult("EndDate must be grater than StartDate");
}
Run Code Online (Sandbox Code Playgroud)
请记住,自定义验证是通用的,这就是为什么许多代码和自我验证模型仅适用于所应用的模型的原因.
希望能帮助到你
添加
我没有解释自定义客户端验证部分,可以随意询问您是否需要示例,但基本上:
在MVC3中更容易(当然,如果你理解jQuery.Validate),你需要做的就是:
IClientValidateble要创建这三件事,让我们考虑这一点GreaterThanDateAttribute并创建自定义客户端验证.为此我们需要编写代码:
追加到 GreaterThanDateAttribute
public IEnumerable<ModelCLientValidation> GetCLientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelCLientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "greater"; // This is what the jQuery.Validation expects
rule.ValidationParameters.Add("other", OtherPropertyName); // This is the 2nd parameter
yield return rule;
}
Run Code Online (Sandbox Code Playgroud)
然后你需要编写新的jQuery Validator和元数据适配器,它将jQuery.Validation链接到你的代码,提供该data-字段的正确属性(当然,如果UnobtrusiveJavaScriptEnabled是)
创建一个新js文件并附加到您<head>的例如
<script src="@Url.Content("~/Scripts/customValidation.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
并附加新的验证
jQuery.validator.addMethod("greater", function(value, element, param) {
// we need to take value and compare with the value in 2nd parameter that is hold in param
return Date.parse(value) > Date.parse($(param).val());
});
Run Code Online (Sandbox Code Playgroud)
然后我们编写适配器
jQuery.validator.unobtrusive.adapters.add("greater", ["other"], function(options) {
// pass the 'other' property value to the jQuery Validator
options.rules["greater"] = "#" + options.param.other;
// when this rule fails, show message that comes from ErrorMessage
options.messages["greater"] = options.message;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6819 次 |
| 最近记录: |