Asp.net MVC 3 DataAnnotations条件验证 [英] Asp.net MVC 3 Conditional validation with DataAnnotations
问题描述
我正在使用Asp.net MVC 3,具有dataannotations面临的问题,验证如下
I are using Asp.net MVC 3, facing validation problem with dataannotations as below
我们一直保持着模型,模型类层次结构就像是在
We have maintained model in separate library project, model class hierarchy is like Below
public class EditAlternateMailingAddressModel : BaseModel
{
public UserAddressDetails AlternateAddressDetails { get; set; }
public List<UsState> StateList { get; set; }
}
现在userAddressDetails定义像下面
now userAddressDetails is defined like below
public partial class UserAddressDetails
{
public string DeliveryLine { get; set; }
public string Zip { get; set; }
public bool IsDefaultMailingAddress { get; set; }
}
验证逻辑在不同的类中定义象下面
validation logic are defined in separate class like below
[MetadataType(typeof(UserAddressDetailsMetaData))]
public partial class UserAddressDetails
{
private class UserAddressDetailsMetaData
{
[Required(ErrorMessage = "Please enter address.")]
public string DeliveryLine { get; set; }
[Required(ErrorMessage = "Please enter city.")]
public string City { get; set; }
public bool IsDefaultMailingAddress { get; set;
}
在编辑视图, DeliveryLine,邮编依赖于IsDefaultMailingAddress因为这些字段必须要是IsDefaultMailingAddress是真实的提供,否则我们将提交表单。
打开和部分提交,我们使用jQuery的表格。
for opening and partially submitting the forms we are using jQuery.
我们已经尝试以下
<一href=\"http://andrewtwest.com/2011/01/10/conditional-validation-with-data-annotations-in-asp-net-mvc/\" rel=\"nofollow\">http://andrewtwest.com/2011/01/10/conditional-validation-with-data-annotations-in-asp-net-mvc/
的http://blogs.msdn.com/b/simonince/archive/2010/06/04/conditional-validation-in-mvc.aspx
但这些验证被触发服务器端,我们需要使它在客户端工作。
but these validation are fired server side, we need to make it work on client side.
推荐答案
您应该创建你自己定制的ValidationAttribute。如果你想客户端验证,你的属性应该实现IClientValidatable接口,你应该写定制的客户端验证。
You should create you own custom ValidationAttribute. If you want client validation, your attribute should implement IClientValidatable interface, and you should write custom client side validator.
更新:加code样品
实施验证:
public class CustomRequiredAttribute : ValidationAttribute, IClientValidatable
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// your validation logic here
return ValidationResult.Success;
}
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return new[] {new CustomRequiredValidationRule (ErrorMessage)};
}
}
public class CustomRequiredValidationRule : ModelClientValidationRule
{
public RequiredIfVisibleValidationRule(string errorMessage)
{
ValidationType = "customRequire";
ErrorMessage = errorMessage;
}
}
添加客户端验证:
(function ($) {
$.validator.addMethod('customRequire', function (value, element) {
// your validation logic here
return true; // true if valid, otherwise false
});
$.validator.unobtrusive.adapters.add('customRequire');
})(jQuery);
这篇关于Asp.net MVC 3 DataAnnotations条件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!