3

ModelState.IsValid is returning false for me in my controller. I know that means one or more model errors were found when model binding. My question is how do I see the errors?

I noticed that my particular ModelState has 6 items in it. If I try to do any of these...

ModelState[0].Errors[0].ToString()
ModelState[0].Errors[0].ErrorMessage
ModelState[0].Value.AttemptedValue

I get this error:

The best overloaded method match for 'System.Web.Mvc.ModelStateDictionary.this[string]' has some invalid arguments
2
  • Have you debugged your controller action and actually seen what's inside ModelState? Commented Oct 15, 2009 at 13:15
  • Can you check the .InnerException of these? Commented Oct 15, 2009 at 13:16

2 Answers 2

3

The indexor into the ModelState is a string (usually the property name of the offending model or name of the html element).

If you check the MSDN docs for the ModelState class, you'll see that it has an Errors collection that will allow you to iterate over the error items (which are ModelError instances) to see what caused them.

Sign up to request clarification or add additional context in comments.

Comments

2

in controller;

ModelState.AddModelError("username", "Bad username");

in view;

 <%= Html.ValidationMessage("username") %>

also

<%= Html.ValidationSummary() %>

Html.ValidationSummary() might be what you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.