Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC2: ModelState is invalid, but I don't know why

I'm using MVC2 with data annotations for validation on my page. I require a name and a valid birth date to be present. I'm providing both, and I break into the date validator to ensure that the birth date is valid (and it is), but for some reason Model.IsValid is false in my post action. I'm not seeing anything in my ValidationSummary.

How can I find out what is invalid in the ModelState?

like image 901
Mike Pateras Avatar asked Jan 04 '10 18:01

Mike Pateras


1 Answers

You need to iterate through the ModelState collection checking the ModelState.Errors collection count for each property is greater than 0. To get the collection of modelstate items in error, something like

ModelState["Property"].Where(ms => ms.Errors.Count > 0)

Kindness,

Dan

like image 98
Daniel Elliott Avatar answered Sep 28 '22 08:09

Daniel Elliott