ViewData 和 TempData 的区别? ?

我知道 ViewData 是什么,并且一直在使用它,但是在 ASP.NET Preview 5中,他们引入了一个新的东西,叫做 TempData。

我通常强类型化我的 ViewData,而不是使用对象字典的方法。

那么,什么时候应该使用 TempData 而不是 ViewData 呢?

对此有什么最佳实践吗?

48751 次浏览

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.

Although outdated, this article has good description of the TempData lifecycle.

As Ben Scheirman said here:

TempData is a session-backed temporary storage dictionary that is available for one single request. It’s great to pass messages between controllers.

When an action returns a RedirectToAction result it causes an HTTP redirect (equivalent to Response.Redirect). Data can be preserved in the TempData property (dictionary) of the controller for the duration of a single HTTP redirect request.

I found this comparison useful: http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html

One gotcha I came across is that TempData values are cleared after they are read by default. There are options, see methods 'Peek' and 'Keep' on Msdn for more info.

ViewData:

  • ViewData is a dictionary type public ViewDataDictionary ViewData { get; set; }
  • It can be used to pass data from controller to view, one way only
  • It’s life lies only during the current request
  • If passing string then no need to typecast
  • If passing object then you need to typecast it but before that you need to check if it is not null
  • Its a property on ControllerBase, which is the parent of Controller class

TempData:

  1. TempData internally use TempDataDictionary: public TempDataDictionary TempData { get; set; }
  2. Once data is saved into TempDataDictionary object:
    • It persists in it and can be read from any view or any action in any controller
    • It can only be read once; once read, it becomes null
    • It is saved into session so on expiration of session data is lost.

This behavior is new from ASP.NET MVC 2 and latter versions. In earlier versions of ASP.NET MVC, the values in TempData were available only until the next request.

  1. It’s alive, until it is read or session expires and can be read from anywhere.

See the comparison of ViewData, ViewBag, TempData and Session in MVC in detail

view data is used when we want to pass data from controller to corresponding view. view data have very short life it means it will destroy when redirection occurs. Example(Controller):

public ViewResult try1()
{
ViewData["DateTime"] = DateTime.Now;
ViewData["Name"] = "Mehta Hitanshi";
ViewData["Twitter"] = "@hitanshi";
ViewData["City"] = "surat";
return View();
}

try1.cshtm

<table>
<tr>
<th>Name</th>
<th>Twitter</th>
<th>Email</th>
<th>City</th>
<th>Mobile</th>
</tr>
<tr>
<td>@ViewData["Name"]</td>
<td>@ViewData["Twitter"]</td>
<td>@ViewData["City"]</td>
</tr>
</table>

TempData transfers the data between controllers or between actions. It is used to store one time messages and its life is very short.we can use TempData.Keep() to make it available through all actions or to make it persistent.

Example(Controller):

public ActionResult try3()
{
TempData["DateTime"] = DateTime.Now;
TempData["Name"] = "Ravina";
TempData["Twitter"] = "@silentRavina";
TempData["Email"] = "Ravina12@gmail.com";
TempData["City"] = "India";
TempData["MobNo"] = 9998975436;
return RedirectToAction("TempView1");
}
public ActionResult TempView1()
{
return View();
}

TempView1.cshtm

<table>
<tr>
<th>Name</th>
<th>Twitter</th>
<th>Email</th>
<th>City</th>
<th>Mobile</th>
</tr>
<tr>
<td>@TempData["Name"]</td>
<td>@TempData["Twitter"]</td>
<td>@TempData["Email"]</td>
<td>@TempData["City"]</td>
<td>@TempData["MobNo"]</td>
</tr>
</table>

Just a side note to TempData.
Data in it is stored not stored until the next request, but until the next read operation is called!
See:
TempData won't destroy after second request