【问题标题】:How to add Custom logic for validation using asp.net mvc data annotations?如何使用 asp.net mvc 数据注释添加自定义逻辑进行验证?
【发布时间】:2010-07-13 19:39:46
【问题描述】:

我之前读过 this 关于 asp.net mvc 验证,但没有提到我想做什么,所以有这个视图模型 -

public class MyViewModel
{
        [StringLength(200, MinimumLength = 2, ErrorMessage = "Invalid Name")]
        public string Name { get; set; }
        [Required(ErrorMessage = "*")]
        public DateTime StartDate { get; set; }
        [Required(ErrorMessage = "*")]
        public DateTime EndDate { get; set; }

}

我已经设置了验证并且它可以工作..但是现在我想添加一个条件,比如 StartDate 应该总是大于 End Date..我怎样才能添加这样的自定义逻辑/验证?而不是显式地在控制器上检查它并重定向...... asp.net mvc 验证可以容纳这样的东西吗?

【问题讨论】:

    标签: asp.net-mvc-2 data-annotations customvalidator


    【解决方案1】:

    这就是我最终做的事情,我也在检查选择的日期是否不是今天..以防有人想做类似的事情 -

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
        public sealed class EndDateValidationAttribute : ValidationAttribute
        {
            private const string _defaultErrorMessage = "End date cannot be prior to start date";
    
            public EndDateValidationAttribute(string startDate, string endDate)
                : base(_defaultErrorMessage)
            {
                StartDateStr = startDate;
                EndDateStr = endDate;
                ErrorMessage = _defaultErrorMessage;
            }
    
            public string StartDateStr { get; private set; }
            public string EndDateStr { get; private set; }
    
            public DateTime StartDate { get; private set; }
            public DateTime EndDate { get; private set; }
    
            public override bool IsValid(object value)
            {
                // This is not a required field validator, so if the value equals null return true.  
                if (value == null) return true;
    
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
                object startDate = properties.Find(StartDateStr, true /* ignoreCase */).GetValue(value);
                object endDate = properties.Find(EndDateStr, true /* ignoreCase */).GetValue(value);
    
                StartDate = (DateTime)startDate;
                EndDate = (DateTime)endDate;
    
                if (StartDate > EndDate) return false;
                else if (Convert.ToDateTime(startDate) == DateTime.Today.Date)
                {
                    return false;
    
                }
                return true;
            }
        } 
    

    这里是你如何使用它 -

    [EndDateValidationAttribute("StartDate", "EndDate", ErrorMessage = "Start date should be after today's date and before end date!")]
        public class CustomeDate
        {
            [DisplayName("StartDate")]
            [Required(ErrorMessage = "*")]
            public DateTime StartDate { get; set; }
            [DisplayName("EndDate")]
            [Required(ErrorMessage = "*")]
            public DateTime EndDate { get; set; }
        }
    

    【讨论】:

      【解决方案2】:

      对此我不确定,但可能值得查看默认的 asp.net MVC 帐户控制器和 PropertiesMustMatchAttribute 以获取自定义验证示例。

      简单看了下代码,应该是可行的。

      【讨论】:

        猜你喜欢
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多