我有一个包含三个下拉列表(mm、dd、yyyy)的日期编辑器模板。下面是相同的类,其中包含一些正常工作的验证
public class DateDDL
{
public IEnumerable<SelectListItem> Days { get; set; }
public IEnumerable<SelectListItem> Months { get; set; }
public IEnumerable<SelectListItem> Years { get; set; }
[Attributes.DateDDLValidation("Day", "Month", "Year", ErrorMessage = "Select day")]
public int Day { get; set; }
[Attributes.DateDDLValidation("Day", "Month", "Year", ErrorMessage = "Select month")]
public int Month { get; set; }
[Attributes.DateDDLValidation("Day", "Month", "Year", ErrorMessage = "Select year")]
public int Year { get; set; }
}
但是现在我必须在模型类中为同一个编辑器编写一个验证,如下所示
public partial class User
{
public DateDDL DOBDDL { get; set; }
[pmrms.Attributes.DateGreaterThanToday(ErrorMessage="testting")]
public DateDDL JoinedOnDDL { get; set; }
}
但是对此的客户端验证无法设置或无法正常工作。GetClientValidationRules 方法未在自定义验证器类中加载
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return new ValidationResult(ErrorMessage);
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new[] { new ModelClientValidationDateGreaterThanToday(ErrorMessage) };
}
public class ModelClientValidationDateGreaterThanToday : ModelClientValidationRule
{
public ModelClientValidationDateGreaterThanToday(string errorMessage)
{
ErrorMessage = errorMessage;
}
}