Friday, September 16, 2016

Displaying ModelState Validation errors in ASP.NET MVC

If you want to display ModelState Validation errors (ex: ModelState.AddModelError...) in your ASP.NET MVC Views, the easiest way to display them is to use the @Html.ValidationSummary() or @Html.ValidationSummary(false) Html Helpers.

However, if you add specific keys to your ModelState that you want to display outside of a ValidationSummary control, you can do something like the following:

 ModelState.AddModelError("MyCustomKey", "My Custom error");

Then, in order to display it in your view, you can simply use the @Html.ValidationMessage Razor Html Helper as follows:


That is all there is to it!

No comments:

Post a Comment