Thursday, April 2, 2015

ModelState Errors not displaying in MVC Razor View

I was recently working on an ASP.NET MVC Project and I was adding ModelState errors in my Controller method and returning the resultant model to my View.

In my Razor View, I had the following code:
@Html.ValidationSummary(true)


However, even though my ModelState error was being added and returned to the View, the View was not displaying my error message!!

Well, as it turns out, I had to change my code to the following in my Razor View:



@Html.ValidationSummary(false)

 

I was inadvertently ignoring all other errors and forcing the ValidationSummary to only display model-level errors. 


As soon as I did this, I could suddenly view my ModelState error message!

 

You can refer to this article for a description of the differences between the 2 options: https://msdn.microsoft.com/en-us/library/ee839469%28v=vs.118%29.aspx


No comments:

Post a Comment