【发布时间】:2016-06-26 13:23:05
【问题描述】:
所以我有这个问题。
我有 2 个字段是 Date of birth 和 Start working date。如果
开始工作日期 - 出生日期 >= 22
然后是有效的。所以这是我的代码
[AttributeUsage(AttributeTargets.Property)]
public class MiniumAgeAttribute:ValidationAttribute
{
private DateTime dob { get; set; }
private DateTime startDate { get; set; }
public MiniumAgeAttribute(DateTime DOB, DateTime StartDate)
{
dob = DOB;
startDate = StartDate;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
int age;
age = startDate.Year - dob.Year;
if (age >= 22)
{
return ValidationResult.Success;
}
else
{
return new ValidationResult("Age is required to be 22 or more");
}
}
}
但是当我在模型中应用我的验证规则时,我得到了这个错误
那么我该如何解决它。 亲切的问候。
【问题讨论】:
标签: asp.net-mvc validation model