2011-08-11 32 views
7

Tôi đang sử dụng Asp.net MVC 3, đối diện với vấn đề xác nhận với dataannotations như mô hình dưới đâyAsp.net MVC 3 xác nhận có điều kiện với DataAnnotations

Chúng tôi đã duy trì trong dự án thư viện riêng biệt, hệ thống phân cấp mô hình lớp giống như Dưới

public class EditAlternateMailingAddressModel : BaseModel 
{ 
    public UserAddressDetails AlternateAddressDetails { get; set; } 

    public List<UsState> StateList { get; set; }   
} 

tại userAddressDetails được định nghĩa như dưới đây

public partial class UserAddressDetails 
    { 
     public string DeliveryLine { get; set; } 
     public string Zip { get; set; } 
     public bool IsDefaultMailingAddress { get; set; }   
    } 

logic xác nhận được định nghĩa trong lớp riêng biệt như dưới đây

[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; 
} 

trong chỉnh sửa xem, DeliveryLine, Zip phụ thuộc vào IsDefaultMailingAddress như các lĩnh vực này phải được cung cấp nếu IsDefaultMailingAddress là đúng, ngược lại để cho hình thức phải nộp.

để mở và gửi một phần biểu mẫu chúng tôi đang sử dụng jQuery.

Chúng tôi đã cố gắng bên dưới 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

nhưng những xác nhận được bắn phía máy chủ, chúng ta cần phải làm cho nó hoạt về phía khách hàng.

+0

Bạn đã thấy http://stackoverflow.com/questions/ 4833280/asp-net-mvc-3-unobtrusive-custom-client-validation? – Jack

Trả lời

2

Bạn nên tạo riêng cho bạn Thuộc tính xác thực tùy chỉnh. Nếu bạn muốn xác thực ứng dụng khách, thuộc tính của bạn nên triển khai thực hiện giao diện IClientValidatable và bạn nên viết trình duyệt tính hợp lệ của phía khách hàng tùy chỉnh.

Cập nhật: mẫu mã thêm

Thực hiện validator:

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; 
    } 
} 

Thêm client-side validator:

(function ($) { 
    $.validator.addMethod('customRequire', function (value, element) { 
     // your validation logic here 
     return true; // true if valid, otherwise false 
    }); 
    $.validator.unobtrusive.adapters.add('customRequire'); 
})(jQuery); 
+0

Related http://stackoverflow.com/questions/7025198/mvc3-custom-validation-compare-two-dates – hazzik

Các vấn đề liên quan