A Model contains, amongst other properties, a method which returns an MSChart as a MemoryStream.
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.
In the Model
public MemoryStream ViewerChart()
{
Chart chart = new Chart();
:
:
using (MemoryStream memStream = new MemoryStream())
{
chart.SaveImage(memStream, ChartImageFormat.Jpeg);
return memStream;
}
}
In the View
@{
TempData["Chart"]= Model.ViewerChart();
}
<img alt="Chart" src="@Url.Action("RenderChart")" />
In the Controller
public ActionResult RenderChart()
{
MemoryStream ms = TempData["Chart"] as MemoryStream;
return File(ms.ToArray(), "image/jpeg");
}
Despite working OK, this all seems a bit nasty to me, particularly the use of TempData
Is there a better way?