代码之家  ›  专栏  ›  技术社区  ›  Felix

在dataAnnotations数据类型属性中忽略错误消息

  •  5
  • Felix  · 技术社区  · 15 年前

    我有一个使用数据注释的模型。类似的东西

    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中的值;也就是说,如果不输入值,我将收到“请输入”消息。但是,如果我在日期时间字段中输入了一个字符串,则会收到一条标准的系统错误消息:“值'blah'对于appointmentdate无效”。

    我通过ASP.NET MVC代码进行了调试,在FormatException的情况下,它似乎没有从PropertyMetadata中选择正确的显示名称。要么,要么我遗漏了一些显而易见的东西:/

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

    3 回复  |  直到 14 年前
        1
  •  2
  •   Pawel Krakowiak    15 年前

    在mvc1中,数据类型属性并不像您期望的那样,看起来它也不在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);
                }
            }
        }
    

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

        2
  •  2
  •   user781861    14 年前

    这个链接可能会帮到你。提供有关将验证连接到数据类型的说明(非默认行为)

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

        3
  •  1
  •   Mayo    14 年前

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

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

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