如何在Asp.Net MVC中显示DisplayFormat属性的自定义错误消息?

Dig*_*mar 5 data-annotations asp.net-mvc-3

如何显示自定义错误消息

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
Run Code Online (Sandbox Code Playgroud)

我的模型中的代码如下,

[Display(Name = "When was that document issued?")]
[DataType(DataType.Date, ErrorMessageResourceName = "DocumentIssueDate_DataType",ErrorMessageResourceType = typeof(CustomErrorMessages))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
 public DateTime? DocumentIssueDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的视图中,当我在日期文本框中输入201时,我收到以下错误消息.如何修改以下错误消息.

值"201"对DocumentIssueDate无效.

Fri*_*gik 0

尝试在这里查看答案。我认为 DisplayFormat 的设计不是为了验证,这就是为什么你不能用它来检查输入的字符串是否有效。