我将这些视图模型组合在一起,以某种形式使用。但是,当我验证表单并将“电子邮件”,“电话号码”和“方法”下拉列表保留为空白时,将不显示任何验证,并且在调试部分中,ModelState有效。
由于某些原因,下拉列表似乎也没有返回任何验证错误。当我调试时,输入是空的,这是有意的,但不会显示任何错误
这些是视图模型:
public class UnsubscribeContactDetailsViewModel
{
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
public class MethodsDropdownListViewModel
{
public string Method { get; set; }
public IEnumerable<SelectListItem> Methods { get; set; }
}
public class RegisterUnsubscribeViewModel : IValidatableObject
{
public string Id { get; set; }
[Required]
public UnsubscribeContactDetailsViewModel ContactDetails { get; set; }
public MethodsDropdownListViewModel MethodsDropdownList { get; set; }
public DateTime CreatedOn { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (string.IsNullOrWhiteSpace(ContactDetails.Email) && string.IsNullOrWhiteSpace(ContactDetails.PhoneNumber))
{
yield return new ValidationResult("Either Email or Phone Number is required");
}
}
}