3

您如何获得模型的验证以同时验证通用列表属性中的子对象。

我有一个我正在尝试验证的模型,这不是发布到服务器的内容,而是发布的一些信息的组合,以及服务器上已经存在的信息......例如。

 ...
public class A {
   [Required]
   public string Property1 { get; set; }
}
...
public class B {
   public List<A> Values { get; set; }
}
...
    if (!TryValidateModel(instanceofB))
    {
        //this should fire, as one of A inside B isn't valid.
        return View(instanceofB);
    }

当我尝试验证 B 的模型实例时,它不会验证 Values 集合的验证属性。

4

2 回答 2

5

TryValidateModel方法只下降了一层,因此它只检查Validationtype 对象的属性B,而不是其嵌套对象。克服这个问题的一种方法是定义您自己的 a 实现ValidationAttribute

public class ListValidationAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        IEnumerable enumerable = value as IEnumerable;
        // If the input object is not enumerable it's considered valid.
        if (enumerable == null)
        {
            return true;
        }
        foreach (object item in enumerable)
        {
            // Get all properties on the current item with at least one
            // ValidationAttribute defined.
            IEnumerable<PropertyInfo> properties = item.GetType().
                GetProperties().Where(p => p.GetCustomAttributes(
                typeof(ValidationAttribute), true).Count() > 0);
            foreach (PropertyInfo property in properties)
            {
                // Validate each property.
                IEnumerable<ValidationAttribute> validationAttributes =
                    property.GetCustomAttributes(typeof(ValidationAttribute),
                    true).Cast<ValidationAttribute>();
                foreach (ValidationAttribute validationAttribute in
                    validationAttributes)
                {
                    object propertyValue = property.GetValue(item, null);
                    if (!validationAttribute.IsValid(propertyValue))
                    {
                        // Return false if one value is found to be invalid.
                        return false;
                    }
                }
            }
        }
        // If everything is valid, return true.
        return true;
    }
}

现在List<A>可以使用属性进行验证:

public class B
{
    [ListValidation]
    public List<A> Values { get; set; }
}

我没有彻底测试上述方法的性能,但如果在你的情况下结果是一个问题,另一种方法是使用辅助函数:

    if (!ValidateB(instanceofB))
    {
        //this should fire, as one of A inside B isn't valid.
        return View(instanceofB);
    }

...

public bool ValidateB(B b)
{
    foreach (A item in b.Values)
    {
        if (!TryValidateModel(item))
        {
            return false;
        }
    }
    return true; 
}
于 2010-12-20T11:34:48.157 回答
1

我有一个类似的问题,我通过完全避免调用 TryValidate 来解决。我调用 TryValidate 的原因是因为我需要对我的模型进行一些更改,然后进行验证。我最终为模型创建了一个接口,并将默认模型绑定器替换为能够识别该接口并调用我的方法的模型绑定器。这一切都发生在框架调用 validate 第一次(这是递归的)之前。

于 2012-11-22T14:05:38.593 回答