加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 如何在Asp.Net MVC中显示DisplayFormat属性的

发布时间:2020-12-16 03:21:20 所属栏目:asp.Net 来源:网络整理
导读:如何显示自定义错误消息 [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:d}")] 我的模型中的代码如下, [Display(Name = "When was that document issued?")][DataType(DataType.Date,ErrorMessageResourceName = "DocumentIssueDate_D
如何显示自定义错误消息

[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:d}")]

我的模型中的代码如下,

[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; }

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

The value ‘201’ is not valid for DocumentIssueDate.

解决方法

经过大量的研究,我没有运气.唯一的解决方案是使用RegularExpression.

而不是使用:

[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:d}")]

您可以使用:

[RegularExpression(@"^(3[01]|[12][0-9]|0[1-9])[-/](1[0-2]|0[1-9])[-/][0-9]{4}$",ErrorMessageResourceName = "Date Not Valid")]

RegularExpression与日期格式dd / MM / yyyy匹配.

常规仍然没有测试是,但你可以搜索和编辑,如果需要

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读