我将我的项目从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 {}
答案 0 :(得分:0)
解决方案是将AttributeTargets.Class更改为AttributeTargets.Property并重写每个验证器以使用给定的属性。