我不确定标题是否清楚。让我更好地解释一下:
我有两个模型:
1-GuestResponse.cs:
public class GuestResponse
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage="Please enter a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your phone number")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please especify wheter you will attend")]
public bool? WillAttend { get; set; }
}
2- CompanyGuestResponse.cs:
public class CompanyGuestResponse
{
[Required(ErrorMessage = "Please enter your company name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter your company email address")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your company phone number")]
public string Phone { get; set; }
public IList<GuestResponse> GuestResponses { get; set;}
}
我还有一个用于 CompanyGuestResponse 模型的强类型视图,其中有一个带有表格的表单,可以让用户为属性 GuestResponses 提供输入。
我希望只有在正确验证了表格的填充行(即,不要尝试验证未填充的行)并且至少有一行正确填充的情况下才能提交表单。
我试图通过创建一个自定义注释来做到这一点,如ViewModel 验证中所述的 List没有成功(mvc 只是忽略了列表注释并验证了 GuestResponse 类中的注释):
[EnsureOneElementAttribute(ErrorMessage = "At least a person is required")]
public IList<GuestResponse> GuestResponses { get; set;}
结果: