我正在使用System.ComponentModel.DataAnnotations命名空间来验证我的域类.如何创建自定义属性来验证属性的唯一性,而不管数据库(例如通过某个接口)?
我在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)?
c# attributes fluentvalidation data-annotations asp.net-mvc-2
我的类中有一个int属性,并想验证用户是否输入了一个字符串.如何使用数据注释来做到这一点?当我传递一个非整数值时,我得到一个像这样的excpetion:
The value 'asdasd' is not valid for property.
Run Code Online (Sandbox Code Playgroud)
例如,使用此验证属性:
[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }
Run Code Online (Sandbox Code Playgroud)
并在使用该模型的字段中输入'aaa'我已经获得了此消息的例外情况:
The value 'aaa' is not valid for Number.
Run Code Online (Sandbox Code Playgroud)
而不是"无效的数字"消息.
有任何想法吗?
我已经把
[Range(0, Int32.MaxValue, ErrorMessage="Invalid Number")]
public int? Number { get; set; }
Run Code Online (Sandbox Code Playgroud)
但我从这个消息中得到了这个消息
The value 'aaa' is not valid for Number.
Run Code Online (Sandbox Code Playgroud)