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