我有自定义验证属性,如下所示:
public class MyCustomAttribute : ValidationAttribute {
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
if ((int)value == 100) {
// do some checking to validate & return ValidationResult accordingly
} else return ValidationResult.Success;
}
}
在使用中是这样的:
[DisplayName("My Custom Property")]
[MyCustom(ErrorMessage = "ERROR!!!")]
public int? MyCustomProperty { get; set; }
我的问题是:为什么在 MyCustomAttribute 中的 IsValid 方法中,validationContext 始终为 NULL?我需要设置什么特殊的东西才能使其不为空吗?
如果你使用
ValidationResult IsValid(object value, ValidationContext validationContext)
要检查数据是否有效,您必须使用
v.GetValidationResult(propertyValue,new ValidationContext(this))!= ValidationResult.Success
代替
v.IsValid(propertyValue)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)