ASP.NET MVC Model State

ModelState.IsValid returns false for me in my controller. I know that this means that one or more model errors were detected while binding the model. My question is: how do I see errors?

I noticed that my specific ModelState has 6 elements. If I try to do any of this ...

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
+3
source share
2 answers

An indicator in ModelState is a string (usually the name of the property of the abusive model or the name of the html element).

MSDN ModelState, , , ( ModelError ), , .

+3

;

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

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

<%= Html.ValidationSummary() %>

Html.ValidationSummary() , .

+2

Source: https://habr.com/ru/post/1720179/


All Articles