我把我的项目从MVC2升级到了MVC3,所有的自定义ValidationAttributes都坏了。在模型绑定器阶段的某个时刻,它抛出了这个异常:
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: value
System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value) 我不知道是什么改变了..。没有DisplayName..
我怀疑IsValid覆盖,它可能以某种方式改变了,但签名是相同的。
public override bool IsValid(object value)
{
....
}更多信息:
它显然会影响"AttributeTargets.Class“属性,如下所示:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class MyValidationAttribute : ValidationAttribute {}发布于 2011-02-01 05:57:59
解决方案是将AttributeTargets.Class更改为AttributeTargets.Property,并重写每个验证器以使用给定的属性。
https://stackoverflow.com/questions/4762890
复制相似问题