I've got a controller action like this:
[ActionName("Create"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save([Bind(Exclude="Id")]Project project)
{
try
{
_projectRepository.Save(project);
return RedirectToAction("Details", new { id = project.Id });
}
catch (PropertyValueException ex)
{
ModelState.AddModelError(ex.PropertyName, ex.Message);
return View();
}
catch (Exception ex)
{
ModelState.AddModelError("", ex);
return View();
}
}
The code to handle PropertyValueException
works fine: the error is displayed by Html.ValidationSummary
, and the error is displayed next to the relevant field correctly.
With the second catch block, I was attempting to cater for (e.g.) duplicate constraint violations, and I wanted the exception to be displayed in the validation summary (it can't be displayed next to a particular field, because I don't have that information). It doesn't work.
What am I doing wrong?