加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在DataAnnotations DataType Attribute中忽略Err

发布时间:2020-12-16 06:56:41 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用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,E
我有一个使用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; }
}

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

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

有人遇到过这个问题吗?是我,还是只是beta(我使用的是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);
            }
        }
    }

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读