2
即使在刪除所需的屬性後,爲什麼我得到「出生日期是必需的」錯誤消息?是否因爲任何其他屬性?我該如何改變它?我只是檢查一下,如果有人提出了不尋常的年齡,我可以找到它。MVC中的數據註釋5
namespace ProjectCrux.Models
{
public class Student
{
public int studentId { get; set; }
[Display(Name = "Date of Birth")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[DateOfBirth(MinAge = 15, MaxAge = 90, ErrorMessage = "Too young or too old???")]
public DateTime dateOfBirth { get; set; }
public string salt { get; set; }
}
/*
* Attribute to validate date of birth within a range
*/
public class DateOfBirthAttribute : ValidationAttribute
{
public int MinAge { get; set; }
public int MaxAge { get; set; }
public override bool IsValid(object value)
{
if (value == null)
return true;
var val = (DateTime)value;
if (val.AddYears(MinAge) > DateTime.Now)
return false;
return (val.AddYears(MaxAge) > DateTime.Now);
}
}
}