asp.net-mvc – 在DataAnnotations DataType Attribute中忽略Err
发布时间:2020-12-16 06:56:41 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用DataAnnotations的模型.就像是 public class Appointment { [Required(ErrorMessage="Please enter your name")] public string Name { get; set; } [Required(ErrorMessage="Please enter your appointment date?")] [DataType(DataType.Date,E
我有一个使用DataAnnotations的模型.就像是
public class Appointment { [Required(ErrorMessage="Please enter your name")] public string Name { get; set; } [Required(ErrorMessage="Please enter your appointment date?")] [DataType(DataType.Date,ErrorMessage="Appointment date is not a date")] public DateTime AppointmentDate { get; set; } } “Required”属性遵循ErrorMessage中的值;也就是说,如果我没有输入值,我会收到“请输入”消息.但是,如果我在DateTime字段中输入一个字符串,我收到标准系统错误消息“值’blah’对AppointmentDate无效”. 我通过ASP.NET MVC代码调试,似乎在FormatException的情况下,它没有从propertyMetadata中选择正确的显示名称.不管怎样,或者我遗漏了一些明显的东西:/ 有人遇到过这个问题吗?是我,还是只是beta(我使用的是ASP.NET MVC 2 Beta)? 解决方法
在MVC1中,DataType属性没有达到你所期望的效果,看起来它在MVC2中也没有.你最好的电话是拥有一个表示日期的字符串属性,在那里检查它的有效性.
这是项目的一小段摘录(使用DataAnnotations和xVal): private List<ErrorInfo> _errors; private List<ErrorInfo> Errors { get { if (_errors == null) _errors = new List<ErrorInfo>(); return _errors; } //set { _errors = value; } } private string _startDateTimeString; /// <summary> /// A string reprsentation of the StartDateTime,used for validation purposes in the views. /// </summary> /// <remarks> /// If the user passes something like 45/45/80 it would be a valid mm/dd/yy format,but an invalid date,/// which would cause an InvalidOperationException to be thrown by UpdateModel(). By assigning dates to string properties /// we can check not only the format,but the actual validity of dates. /// </remarks> public string StartDateTimeString { get { return _startDateTimeString; } set { // Check whether the start date passed from the controller is a valid date. DateTime startDateTime; bool validStartDate = DateTime.TryParse(value,out startDateTime); if (validStartDate) { StartDateTime = startDateTime; } else { Errors.Add(new ErrorInfo("StartDateTime","Provide a valid date for the start time.")); } _startDateTimeString = value; } } partial void OnValidate(ChangeAction action) { if (action != ChangeAction.Delete) { Errors.AddRange(DataAnnotationsValidationRunner.GetErrors(this)); if (StartDateTimeString != null) { DateTime startDateTime; if (!DateTime.TryParse(StartDateTimeString,out startDateTime)) { Errors.Add(new ErrorInfo("StartDateTime","Provide a valid date for the start time.")); } } if (Errors.Any()) throw new RulesException(Errors); } } } 在我们项目的两个地方进行检查是有意义的,但我只想向您展示一个概念. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何在MVC3中的数据库中提交数据后清除模型
- asp.net-web-api – oData $count不能与web api 4中的Entit
- asp.net-mvc – 从Application_Start中获取网站的URL?
- asp.net-mvc – NHibernate – 懒惰地初始化一个角色集合
- asp.net-mvc-4 – 自定义多态模型绑定程序不绑定派生类型的
- ASP.NET MVC – 使用jQuery不显眼的验证来阻止提交无效表单
- asp.net – 屏幕读者测试网站的可访问性
- asp.net-mvc-3 – 如何验证HTML输入以防止XSS?
- 关于Expression Tree和IL Emit的所谓的"性能差别"
- asp.net-mvc-4 – ASP.Net MVC 4和WebSecurity – 通过电子
推荐文章
站长推荐
- asp.net – 防止用户在同一行上工作
- asp.net-mvc-3 – 在哪里可以找到MvcTextTemplat
- asp.net – Web API外部承载未经授权
- asp.net-mvc – 为什么DisplayFormat DataFormat
- asp.net – 公共访问本地IIS服务器
- asp.net-mvc-3 – MVC3 MapRoute,带斜杠的参数
- asp.net-core – ASP.NET Core WebAPI默认路由不
- asp.net – 为什么REST API会调用错误的方法?
- asp.net – 何时使用Request.RegisterForDispose
- asp.net-mvc – ASP.NET MVC DropDownListFor不支
热点阅读