Question

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 {}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top