DataAnnotations的DataType属性中会忽略ErrorMessage(ErrorMessage is ignored in DataAnnotations DataType Attri

编程入门 行业动态 更新时间:2024-10-25 20:25:45
DataAnnotations的DataType属性中会忽略ErrorMessage(ErrorMessage is ignored in DataAnnotations DataType Attribute)

我有一个使用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; } }

“必需”属性尊重ErrorMessage中的值; 也就是说,如果我没有输入数值,我会收到我的“请输入”消息。 但是,如果我在DateTime字段中输入字符串,则会收到标准系统错误消息“值'blah'对于AppointmentDate无效”。

我通过ASP.NET MVC代码进行调试,并且似乎在FormatException的情况下,它不会从propertyMetadata中选择正确的显示名称。 要么是这样,要么我错过了一些显而易见的东西:/

有人遇到这个问题吗? 是我,还是只是测试版(我正在使用ASP.NET MVC 2 Beta)?

I have a Model that is using DataAnnotations. Something like

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

The "Required" attributes respect the value in ErrorMessage; that is, if I don't enter a value, I am getting my "please enter" message. However, if I enter a string in the DateTime field, I am getting a standard system error message "The value 'blah' is not valid for AppointmentDate".

I debugged through ASP.NET MVC code, and it seems that in the case of FormatException it doesn't pick the right display name from propertyMetadata. Either that, or I am missing something blatantly obvious :/

Did anybody run into this problem? Is it me, or is it just beta (I am using 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); } } }

在我们的项目的两个地方都有这个检查是有意义的,但我只是想告诉你一个概念。

In MVC1 DataType attribute does not do what you'd expect, it looks like it does not in MVC2 either. Your best call is to have a string property representing the date, check it's validity there.

Here's a small excerpt from a project (using DataAnnotations and 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); } } }

It makes sense to have the check in both places in our project, but I just want to show you a concept.

更多推荐

本文发布于:2023-04-28 01:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中会   属性   DataType   DataAnnotations   ErrorMessage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!