通过TempData的的MemoryStream到IMG在MVC3 - 有没有更好的办法? [英] MemoryStream to IMG via TempData in MVC3 - is there a better way?

查看:117
本文介绍了通过TempData的的MemoryStream到IMG在MVC3 - 有没有更好的办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个模型包含,除其他属性,它返回一个MSChart作为一个MemoryStream的方法。

A Model contains, amongst other properties, a method which returns an MSChart as a MemoryStream.

在我看来,我复制到的MemoryStream TempData的[图表],然后用URL.Action()调用的控制器操作时使用的MemoryStream从TempData的返回FileContentResult。

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.

在模型

public MemoryStream ViewerChart()
{
    Chart chart = new Chart();
    :
    :
    using (MemoryStream memStream = new MemoryStream())
    {
        chart.SaveImage(memStream, ChartImageFormat.Jpeg);
        return memStream;
    }
}

在View

@{
    TempData["Chart"]= Model.ViewerChart();
 }
<img alt="Chart" src="@Url.Action("RenderChart")" />

在控制器

 public ActionResult RenderChart()
 {

   MemoryStream ms = TempData["Chart"] as MemoryStream;
   return File(ms.ToArray(), "image/jpeg");
 }

尽管工作正常,这一切似乎有点讨厌我,尤其是使用的TempData的

Despite working OK, this all seems a bit nasty to me, particularly the use of TempData

有没有更好的办法?

推荐答案

acedemic 的办法是在 RenderChart 动作和图表图像返回到视图。

The acedemic way would be to retrieve the model object again in the RenderChart action and return the chart image to the view.

但我认为它在的TempData 图像存储减少数据库往返次数,提高性能的有效方法。

But i think its a valid approach to store the image in TempData for reducing database roundtrips and boosting performance.

也许它甚至不需要提高性能。读它时,第二次,并再次获取数据所需要的附加10ms的可能不是由用户显数据库将具有在其高速缓存中可用的数据。

Maybe its even unnecessary to improve the performance. The database will have the data available in its cache when reading it the second time and the additional 10ms needed to fetch the data again might not be noticeable by the user.

至少的TempData 似乎是合适的店面,因为该值是从集合中删除当它通过阅读 RenderChart 的行动。

At least TempData seems to be the right store because the value is deleted from the collection when its read by the RenderChart action.

这篇关于通过TempData的的MemoryStream到IMG在MVC3 - 有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆