ASP.Net MVC2: ModelState is invalid, but I don't know why
-
18-09-2019 - |
Question
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?
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow