Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a view with it's model from an ActionFilterAttribute
    text
    copied!<p>When implementing error-handling using the built-in validation-helpers on a strongly-typed view, you usually create a try/catch block within the controller and return a view with it's corresponding model as a parameter to the <code>View()</code> method: <br /><br /><br /> <strong>The controller</strong></p> <pre><code>public class MessageController : Controller { [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Models.Entities.Message message) { try { // Insert model into database var dc = new DataContext(); dc.Messages.InsertOnSubmit(message); dc.SubmitChanges(); return RedirectToAction("List"); } catch { /* If insert fails, return a view with it's corresponding model to enable validation helpers */ return View(message); } } } </code></pre> <p><br /><br /> <strong>The view</strong></p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage&lt;Models.Entities.Message&gt;" %&gt; &lt;%= Html.ValidationSummary("Fill out fields marked with *") %&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;div&gt;&lt;%= Html.TextBox("MessageText") %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= Html.ValidationMessage("MessageText", "*") %&gt;&lt;/div&gt; &lt;% } %&gt; </code></pre> <p><br /><br /> I've implemented a simple error-handler in the form of an ActionFilterAttribute, which will be able to either redirect to a generic error view, or redirect to the view which threw an exception, and let the validation-helpers spring to life.</p> <p>Here's how my ActionFilterAttribute looks:</p> <pre><code>public class ErrorLoggingAttribute : ActionFilterAttribute, IExceptionFilter { private Boolean _onErrorRedirectToGenericErrorView; /// &lt;param name="onErrorRedirectToGenericErrorView"&gt; /// True: redirect to a generic error view. /// False: redirect back the view which threw an exception /// &lt;/param&gt; public ErrorLoggingAttribute(Boolean onErrorRedirectToGenericErrorView) { _onErrorRedirectToGenericErrorView = onErrorRedirectToGenericErrorView; } public void OnException(ExceptionContext ec) { if (_onErrorRedirectToGenericErrorView) { /* Redirect back to the view where the exception was thrown and include it's model so the validation helpers will work */ } else { // Redirect to a generic error view ec.Result = new RedirectToRouteResult(new RouteValueDictionary { {"controller", "Error"}, {"action", "Index"} }); ec.ExceptionHandled = true; } } } </code></pre> <p>Redirecting to the view which threw the exception is fairly simple. But here's the kicker: In order for the validation helpers to work, you need to provide the view with it's model.</p> <p>How would you return the view which threw an exception and provide the view with it's corresponding model? (In this case <code>Models.Entities.Message</code>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload