title: Making ModelState Persistent, but not too persistent
body: <p>Rather than a quick fix I'm more just wondering what others are doing.</p>
  
  <p>The problem is that I am using ModelState for error/validation reporting to the user, but when I use a RedirectToAction(), the ModelState is lost.  To overcome this I've added some code to our base controller (that all other controllers inherit), in the override of OnActionExecuting I've added:</p>
  
  <pre><code>if (TempData["ModelState"] != null &amp;&amp; !ModelState.Equals(TempData["ModelState"]))
      ModelState.Merge((ModelStateDictionary)TempData["ModelState"]);
  </code></pre>
  
  <p>And in the override of OnActionExecuted I've added:</p>
  
  <pre><code>TempData["ModelState"] = ModelState;
  </code></pre>
  
  <p>The problem with this is it means some error messages can be displayed on the wrong pages.  I just run a <code>ModelState.clear()</code> at the start of actions I know will want to override/correct the current errors but there are cases where it is never cleared and errors show up in weird places.</p>
  
  <p>How do other people handle this? any ideas welcome. Thanks</p>