自定义验证属性有多个实例问题

Mur*_*ima 5 c# attributes fluentvalidation data-annotations asp.net-mvc-2

我在C#4中使用tha命名空间System.ComponentModel.DataAnnotations来实现我自己的验证属性,它看起来像这样

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class MyCustomValidator : ValidationAttribute {
    private String Property1 { get; set; }
    private String Property2 { get; set; }

    public ValeTaxiSituacaoRequired(String property1, String property2) {
        Property1 = property1;
        Property2 = property2;
    }

    public override bool IsValid(object value) {
        //validation logic
    }

}
Run Code Online (Sandbox Code Playgroud)

我想用这个属性如下

[MyCustomValidator("Name", "Job")]
[MyCustomValidator("Name", "Email")]
[MyCustomValidator("Name", "Job")]
public class Employe {
}
Run Code Online (Sandbox Code Playgroud)

问题是只进行了一次验证.如何执行所有验证(使用asp.net mvc 2)?

Dan*_*son 0

看一下FluentValidation。它允许您将验证与正在验证的类分开,以便您可以随时在服务器或客户端上调用验证逻辑。

它允许您向类中添加任意复杂程度的任意数量的规则,而不会因属性而使其混乱。