This project has moved and is read-only. For the latest updates, please go here.
1
Vote

Non-Nullable Int Issue - missing model properties

description

Given the following viewmodel:

[FluentValidation.Attributes.Validator(typeof(PersonModelValidator))]
public class PersonViewModel
{
public string Name { get; set; }
public int Age { get; set; }
}

And the following Validator:

public class PersonModelValidator : AbstractValidator<PersonViewModel>
{
public PersonModelValidator()
{
    RuleFor(x => x.Name).NotEmpty();
    RuleFor(x => x.Age).NotEmpty();
}
}

If I submit an empty form, after validation, only the Age error ends up in the ModelState error collection.

On the Age property, if I change the int to int?, then both FirstName and Age errors show up in the collection.

What is it about the non-nullable int that affects the validation in this way? Is there some way to capture all the validation erros using a non-nullable int?

comments

JeremyS wrote Mar 5, 2015 at 9:20 AM

Hi

Unfortunately this is one of the (many) limitations of ASP.NET MVC's validation infrastructure.

MVC's DefaultModelBinder will first check to see if the value has been supplied, and if it hasn't, then it will fail validation immediately and never allow model-level validators to run.

The only workaround (as far as I know) is to make the property nullable.