5

我有一个使用 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 测试版)?

4

2 回答 2

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

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

于 2009-11-24T22:02:31.677 回答
1

我今天遇到了这个问题,我想分享另一个解决方法......

[Required(ErrorMessage="Please enter your appointment date?")] 
//[DataType(DataType.Date, ErrorMessage="Appointment date is not a date")]
[Range(typeof(DateTime), "1/1/1880", "1/1/2200", ErrorMessage = "...")]
public string AppointmentDate { get; set; }

您需要调整代码以使用字符串而不是日期时间(如果这是您的视图模型的一部分,这可能很容易),但错误消息按预期工作,并且字符串保证是有效日期(可能带有时间)。

于 2010-10-08T18:37:51.663 回答