ViewData 和 TempData 的区别? [英] Difference Between ViewData and TempData?
问题描述
我知道 ViewData 是什么并且一直在使用它,但是在 ASP.NET Preview 5 中,他们引入了一个叫做 TempData 的新东西.
I know what ViewData is and use it all the time, but in ASP.NET Preview 5 they introduced something new called TempData.
我通常强烈键入我的 ViewData,而不是使用对象字典方法.
I normally strongly type my ViewData, instead of using the dictionary of objects approach.
那么,我什么时候应该使用 TempData 而不是 ViewData?
So, when should I use TempData instead of ViewData?
是否有任何最佳做法?
推荐答案
一句话:TempData
就像 ViewData 有一个区别:它们只包含两个连续请求之间的数据,之后它们被销毁.您可以使用 TempData
传递错误消息或类似信息.
In one sentence: TempData
are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData
to pass error messages or something similar.
虽然过时了,这篇文章TempData
生命周期的描述.
Although outdated, this article has good description of the TempData
lifecycle.
正如 Ben Scheirman 所说 这里:
As Ben Scheirman said here:
TempData 是一个会话支持的临时存储字典,可用于单个请求.在控制器之间传递消息非常棒.
TempData is a session-backed temporary storage dictionary that is available for one single request. It’s great to pass messages between controllers.
这篇关于ViewData 和 TempData 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!