ModelState.AddModelError not showing any message

hetal gala picture hetal gala · Mar 14, 2013 · Viewed 18.6k times · Source

I am using telerik mvc grid. In my table I have unique key defined for a field. And in controller I am catching the error using try ... catch inside DbUpdateException.

in catch block I want to handle the error and show error messsage in view. So using following line,

ModelState.AddModelError("PROGRAM_ID", "Access for this program already exists.");
return View();

But this is not showing error message. Any idea why?

Answer

Darin Dimitrov picture Darin Dimitrov · Mar 14, 2013

Make sure that you have a corresponding ValidationMessage in your view with the same key:

@Html.ValidationMessage("PROGRAM_ID")