DataAnnotations中的ErrorMessage被忽略DataType属性

DataAnnotations中的ErrorMessage被忽略DataType属性

问题描述:

我有一个使用DataAnnotations的模型。类似于DataAnnotations中的ErrorMessage被忽略DataType属性

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)?

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

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

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

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

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

此链接可能会帮助你。提供将验证连接到数据类型的指示(不是默认行为)

http://weblogs.asp.net/srkirkland/archive/2011/02/15/adding-client-validation-to-dataannotations-datatype-attribute.aspx