title: Store complex object in TempData
body: <p>I've been trying to pass data to an action after a redirect by using TempData like so:</p>
  
  <pre><code>if (!ModelState.IsValid)
  {
      TempData["ErrorMessages"] = ModelState;
      return RedirectToAction("Product", "ProductDetails", new { code = model.ProductCode });
  }
  </code></pre>
  
  <p>but unfortunately it's failing with the following message:</p>
  
  <blockquote>
    <p>'<code>System.InvalidOperationException</code> The 
    <code>Microsoft.AspNet.Mvc.SessionStateTempDataProvider'</code> cannot serialize an
    object of type <code>'ModelStateDictionary'</code> to session state.'</p>
  </blockquote>
  
  <p>I've found an issue in <a href="https://github.com/aspnet/Mvc/issues/2276" rel="noreferrer">the MVC project in Github</a>, but while it explains why I'm getting this error, I can't see what would be a viable alternative.</p>
  
  <p>One option would be to serialize the object to a json string and then deserialize it back and reconstruct the <code>ModelState</code>. Is this the best approach? Are there any potential performance issues I need to take into account? </p>
  
  <p>And finally, are there any alternatives for either serializing complex object or using some other pattern that doesn't involve using <code>TempData</code>?</p>