C# 组合两个DataAnnotation验证器(属性)

C# 组合两个DataAnnotation验证器(属性),c#,asp.net-mvc,attributes,data-annotations,C#,Asp.net Mvc,Attributes,Data Annotations,我试图将RequiredIfNot和Range验证器组合起来,但没有成功。 处理方法是:当RequiredIfNot属性的条件为false时,Range属性开始工作,但不工作 实际上,我的代码是 [Range(1, 100, "Out of the range")] [RequiredIfNot("Field", "Value", "This field is required")] public string TVCount { get; set; } 。。。但是范围验证器每次都工作,我希望

我试图将
RequiredIfNot
Range
验证器组合起来,但没有成功。
处理方法是:当
RequiredIfNot
属性的条件为false时,
Range
属性开始工作,但不工作

实际上,我的代码是

[Range(1, 100, "Out of the range")]
[RequiredIfNot("Field", "Value", "This field is required")]
public string TVCount { get; set; }
。。。但是
范围
验证器每次都工作,我希望它只在
RequiredIfNot
条件为false时工作


有没有办法(简单或不简单)做到这一点?

因此,你应该创建自己的验证属性。

因此,你应该创建自己的验证属性。

我认为这是最好的方法,@Agat。谢谢我认为这是最好的办法,阿加特。谢谢