我有一个使用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.
更多推荐
发布评论