serlingpa
serlingpa

Reputation: 12690

ModelState: more information

My ModelState.IsValid is returning false, but I cannot work out why the validation is failing. I have no data annotations on any of the properties of the view model so don't understand.

How do I determine what is making the validation fail? I think only then will I be able to work out what is going on.

Upvotes: 1

Views: 51

Answers (2)

Anupam Singh
Anupam Singh

Reputation: 1166

You can catch errors using :

foreach (ModelState modelState in ViewData.ModelState.Values) {
    foreach (ModelError error in modelState.Errors)
    {        
    // put debug point or add all the errors in collection or list  
    }
}

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156634

Besides the data annotations, errors can be introduced by simple binding issues: a value that's supposed to be a number, but which has letters in it, for example.

The ModelState.Errors property has a collection of error information that you can inspect or iterate across to discover which properties have errors, and what error messages were produced for them.

Upvotes: 2

Related Questions