【问题标题】:.Net MVC2 How to add error to ModelState when using custom ValidationAttribute.Net MVC2如何在使用自定义ValidationAttribute时向ModelState添加错误
【发布时间】:2011-03-10 14:27:09
【问题描述】:

我有以下 ValidationAttribute 类

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class DateValidationAttribute : ValidationAttribute
{
    public DateValidationAttribute(string leftDateProperty, CompareOperator compareOperator, string rightDateProperty, string errorMessage)
            : base(errorMessage)
    {
        LeftDateProperty = leftDateProperty;
        Operator = compareOperator;
        RightDateProperty = rightDateProperty;
    }
    ...
    ...
}

它需要两个日期属性名称和构造函数中的一个运算符。

在验证方法中返回语句LeftDate Operator RightDate的结果。

public override bool IsValid(object value)
{
    DateTime leftDate;
    DateTime rightDate;

    // Get all properties on the view model
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);

    DateTime rightDate = (DateTime)properties.Find(RightDateProperty, true).GetValue(value);
    DateTime leftDate = (DateTime)properties.Find(LeftDateProperty, true).GetValue(value);

    // Perform rule check
    switch (Operator)
    {
        case CompareOperator.Equal:
            return leftDate.Equals(rightDate);
        case CompareOperator.Greater:                    
            return leftDate > rightDate;
        case CompareOperator.Lesser:                    
            return leftDate < rightDate;
        case CompareOperator.GreaterOrEqual:                    
            return leftDate >= rightDate;
        case CompareOperator.LesserOrEqual:                    
            return leftDate <= rightDate;
        default:
            return false;
    }
}

因为这是一个 AttriuteTargets.Class 属性,我知道框架不可能知道导致验证失败的属性。但我知道是左日期属性失败了,因此我想将模型状态中错误的 ID 设置为此属性。这样做的原因是我希望在表单中标记失败的字段。

问题:如何修改ModelState中添加到错误集合中的错误项,使其id对应表单中的特定字段?

【问题讨论】:

    标签: .net asp.net-mvc-2 idataerrorinfo modelstate validationattribute


    【解决方案1】:

    我找到了使用 IDataErrorInfo 的更好方法

    这就是我的做法。它不像问题中的示例那样通用。使用此解决方案,您必须对每项检查进行编码。但是现在验证将一直工作到 javascript 和突出显示失败的输入元素。

    public class TestModel: IDataErrorInfo
    {
    
        public TestModel()
        {
        }
    
        [Required]
        public string StartDate { get; set; }
    
        [Required]
        public string EndDate { get; set; }
    
        #region IDataErrorInfo Members
    
        public string Error
        {
            get
            {
                return string.Empty;
            }
        }
    
        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "StartDate":
                        {
                            if (StartDate < DateTime.Today)
                            {
                                return "Start date must not be a date in the past";
                            }
                            break;
                        }
                    case "EndDate":
                        {
                            if (EndDate < StartDate)
                            {
                                return "End date must not be a date before start date";
                            }
                            break;
                        }   
                    default:
                        return string.Empty;
                }
                return string.Empty;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2018-06-21
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多