开发者

MVC3 ValidationAttribute break

开发者 https://www.devze.com 2023-02-05 20:13 出处:网络
I upgraded my project from MVC2 to MVC3 and all custom ValidationAttributes broke. At some point in the 开发者_如何学Gomodel binder phase, it throws this exception:

I upgraded my project from MVC2 to MVC3 and all custom ValidationAttributes broke. At some point in the 开发者_如何学Gomodel binder phase, it throws this exception:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: value
System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value) 

I can't figure out what changes... there is no DisplayName..

I suspect the IsValid override, which supposedly changed somehow, but the signature is the same.

public override bool IsValid(object value)
{
   ....
}

MORE INFO:

It apparently affects "AttributeTargets.Class" properties, as in:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class MyValidationAttribute : ValidationAttribute {}


The solution was to change AttributeTargets.Class to AttributeTargets.Property and to rewrite each validator to work with a given property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号