Difference Between ViewData and TempData?
asp.net Mvcasp.net Mvc Problem Overview
I know what ViewData is and use it all the time, but in ASP.NET Preview 5 they introduced something new called TempData.
I normally strongly type my ViewData, instead of using the dictionary of objects approach.
So, when should I use TempData instead of ViewData?
Are there any best practices for this?
asp.net Mvc Solutions
Solution 1 - asp.net Mvc
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.
Solution 2 - asp.net Mvc
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.
Solution 3 - asp.net Mvc
ViewData:
ViewData
is a dictionary typepublic 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 ofController
class
TempData:
TempData
internally useTempDataDictionary
:public TempDataDictionary TempData { get; set; }
- 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.
- 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
Solution 4 - asp.net Mvc
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, https://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary">see methods 'Peek' and 'Keep' on Msdn for more info.
Solution 5 - asp.net Mvc
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"] = "[email protected]";
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>
Solution 6 - asp.net Mvc
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:
https://stackoverflow.com/questions/12815739/tempdata-wont-destroy-after-second-request