验证上下文总是NULL? [英] Validation Context is always NULL?
本文介绍了验证上下文总是NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有自定义的验证属性像这样的:
I have custom validation attribute such as this:
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 ?有没有什么特别的,我需要设置得到它不为NULL?
My question is: why is it that inside MyCustomAttribute, within the IsValid method, validationContext is always NULL? Is there anything special I need to set to get it not to be NULL?
推荐答案
如果您使用
ValidationResult IsValid(object value, ValidationContext validationContext)
要检查,如果数据是有效的,你必须用
to check if data is valid you have to use
v.GetValidationResult(propertyValue,new ValidationContext(this))!= ValidationResult.Success
而不是
v.IsValid(propertyValue)
这篇关于验证上下文总是NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文