When implementing error handling using the built-in validation helpers in a strongly typed view, you usually create a try / catch block inside the controller and return the view with the corresponding model as a parameter in the View() method:
Controller
public class MessageController : Controller { [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Models.Entities.Message message) { try {
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.Entities.Message>" %> <%= Html.ValidationSummary("Fill out fields marked with *") %> <% using (Html.BeginForm()) { %> <div><%= Html.TextBox("MessageText") %></div> <div><%= Html.ValidationMessage("MessageText", "*") %></div> <% } %>
I applied a simple error handler in the form of ActionFilterAttribute, which can either redirect to the general idea of โโthe error, or redirect to the view that caused the exception, and let spring helper validators come to life.
This is what my ActionFilterAttribute looks like:
public class ErrorLoggingAttribute : ActionFilterAttribute, IExceptionFilter { private Boolean _onErrorRedirectToGenericErrorView;
Redirecting to a view that throws an exception is pretty simple. But here is the kicker: in order for the validation assistants to work, you need to provide an idea using this model.
How would you return the view that selected the exception and provide the view of the appropriate model? (In this case, Models.Entities.Message ).
source share