public class Dinner
{
public string ID { get; set; }
public string Title { get; set; }
public Category Category { get; set; }
public DateTime? DateCreated { get; set; }
}
该类的模型视图(重要部分)是
public class DinnerModelView
{
...
[UIHint("DatePicker")]
[DateTime(ErrorMessage = "Invalida date")]
public DateTime? DateCreated { get; set; }
}
DateTimeAttriburte 在哪里
public class DateTimeAttribute : ValidationAttribute
{
public DateTimeAttribute () : base (() => "Invalid date") { }
public DateTimeAttribute(string errorMessage) : base(() => errorMessage) { }
public override bool IsValid(object value)
{
if (value == null)
return true;
bool isValid = false;
if (value is DateTime)
isValid = true;
DateTime tmp;
if (value is String)
{
if(String.IsNullOrEmpty((string)value))
isValid = true;
else
isValid = DateTime.TryParse((string)value, out tmp);
}
return isValid;
}
}
但是模型状态错误仍然显示“值‘xxxx’对 DateCreated 无效。” 我无法替换此消息。为什么?