Pergunta

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 {}
Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top