title: How to use HandleError with model state errors
body: <p>I want to use a custom action filter to handle specific exceptions from my service classes to populate the model state and then return the view.</p>
  
  <p>For example, take my previous code:</p>
  
  <pre><code>public ActionResult SomeAction(SomeViewModel model)
  {
      try
      {
          _someService.SomeMethod(model);
      }
      catch (ServiceException ex)
      {
          ModelState.AddModelError(ex.Key, ex.ErrorMessage);
      }
  
      return View();
  }
  </code></pre>
  
  <p>Basically, it would call a service, and if a ServiceException was thrown, it would know that there was an issue w/ the model data, and add the error to the ModelState, then just return the view.  But I noticed some very un-DRY-like patterns, because I had this same try/catch code in every action method.</p>
  
  <p>So, to DRY it up a bit, I basically created a new HandleServiceError action filter:</p>
  
  <pre><code>public class HandleServiceErrorAttribute : HandleErrorAttribute
  {
      public override void OnException(ExceptionContext context)
      {
          ((Controller)context.Controller)
              .ModelState
              .AddModelError(
                  ((ServiceException)context.Exception).Key, 
                  ((ServiceException)context.Exception).ErrorMessage
              );
          context.ExceptionHandled = true;
      }
  }
  </code></pre>
  
  <p>Then simplified my action methods like so:</p>
  
  <pre><code>public ActionResult SomeAction(SomeViewModel model)
  {
      _someService.SomeMethod(model);
      return View();
  }
  </code></pre>
  
  <p>Problem is, once the action filter handles the error, it doesn't return to my action method.  I sort of understand, under the hood, why this is happening.  But I would still like to figure out a way to do what I'm trying to do.</p>
  
  <p>Is this possible?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried the suggestions from the <a href="http://www.asp.net/mvc/tutorials/validating-with-a-service-layer-cs" rel="noreferrer">article Darin provided</a> in his answer, but ran into issues trying to use constructor injection with the controller's model state.  </p>
  
  <p>For example, if you look at their Controllers\ProductController.cs code, they have the controller's empty constructor using a service locator to create the service, passing in the controller's ModelState at that point:</p>
  
  <pre><code>public ProductController() 
  {
      _service = new ProductService(new ModelStateWrapper(this.ModelState), 
                                    new ProductRepository());
  }
  </code></pre>
  
  <p>But if you look at the injected constructor, it assumes the ModelState will be injected into the constructor for the service:</p>
  
  <pre><code>public ProductController(IProductService service)
  {
      _service = service;
  }
  </code></pre>
  
  <p>I don't know how to get CI to work with the current controller's ModelState.  If I could figure this out, then this approach may work.</p>