【发布时间】:2014-12-21 21:20:16
【问题描述】:
即使删除了必需的属性,为什么我会收到“出生日期是必需的”错误消息?是因为其他属性吗?我怎样才能改变它?我只是检查一下,如果有人提出不寻常的年龄,我能找到它。
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);
}
}
}
【问题讨论】:
标签: c# visual-studio model-view-controller