title: MemoryStream to IMG via TempData in MVC3 - is there a better way?
body: <p>A Model contains, amongst other properties, a method which returns an MSChart as a MemoryStream.</p>
  
  <p>In my View I copy the MemoryStream to TempData["Chart"] and then use URL.Action() to call a controllers action to return a FileContentResult using the MemoryStream from TempData.</p>
  
  <p>In the Model</p>
  
  <pre><code>public MemoryStream ViewerChart()
  {
      Chart chart = new Chart();
      :
      :
      using (MemoryStream memStream = new MemoryStream())
      {
          chart.SaveImage(memStream, ChartImageFormat.Jpeg);
          return memStream;
      }
  }
  </code></pre>
  
  <p>In the View</p>
  
  <pre><code>@{
      TempData["Chart"]= Model.ViewerChart();
   }
  &lt;img alt="Chart" src="@Url.Action("RenderChart")" /&gt;
  </code></pre>
  
  <p>In the Controller</p>
  
  <pre><code> public ActionResult RenderChart()
   {
  
     MemoryStream ms = TempData["Chart"] as MemoryStream;
     return File(ms.ToArray(), "image/jpeg");
   }
  </code></pre>
  
  <p>Despite working OK, this all seems a bit nasty to me, particularly the use of TempData</p>
  
  <p>Is there a better way?</p>