Search code examples
asp.net-mvcasp.net-mvc-3validationattributeasp.net-mvc-migration

MVC3 ValidationAttribute break


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 {}

Solution

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