문제

I upgraded my project from MVC2 to MVC3 and all custom ValidationAttributes broke. At some point in the model 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top