DataAnnotations DataType Attribute中忽略ErrorMessage

Fel*_*lix 5 error-handling asp.net-mvc data-annotations

我有一个使用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; }
}
Run Code Online (Sandbox Code Playgroud)

"Required"属性遵循ErrorMessage中的值; 也就是说,如果我没有输入值,我会收到"请输入"消息.但是,如果我在DateTime字段中输入一个字符串,我收到标准系统错误消息"值'blah'对AppointmentDate无效".

我通过ASP.NET MVC代码调试,似乎在FormatException的情况下,它没有从propertyMetadata中选择正确的显示名称.不管怎样,或者我遗漏了一些明显的东西:/

有人遇到过这个问题吗?是我,还是只是beta(我使用的是ASP.NET MVC 2 Beta)?

Paw*_*iak 2

在 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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我们的项目中的两个地方进行检查是有意义的,但我只想向您展示一个概念。