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メソッドは 1 レベルしか下がらないため、ネストされたオブジェクトではなくValidation、 type のオブジェクトの属性のみをチェックします。Bこれを克服する 1 つの方法は、 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 を呼び出した理由は、モデルにいくつかの変更を加えてから検証を行う必要があったためです。最終的にモデルのインターフェイスを作成し、既定のモデル バインダーをインターフェイスを認識してメソッドを呼び出すものに置き換えました。これはすべて、フレームワーク呼び出しが最初に検証する前に発生します (これは再帰的です)。

于 2012-11-22T14:05:38.593 に答える