2
我有自定義驗證屬性像這樣:驗證上下文始終爲NULL?
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?
我知道你的狀態,你是,但我的理解是這是一個MVC 3.0功能。你確定你在使用MVC 3.0? – 2011-03-23 16:46:50
默認情況下,您不需要做任何特殊的事情就可以使其發揮作用。 – Buildstarted 2011-03-23 17:01:23
好的 - 由於某種奇怪的原因,它回覆到2.0的參考(而不是堅持3.0參考)。我現在使用MVC 3.0(按預期)工作。 *嘆。 – 2011-03-23 17:41:13