Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the scope and visibility of TempData in ASP.NET MVC?

Tags:

asp.net-mvc

I'd like to know what the scope and visibility of TempData is in ASP.NET MVC.

like image 711
Vikas Avatar asked Jun 09 '09 12:06

Vikas


People also ask

What is the scope of TempData in MVC?

TempData is a dictionary object to pass the data from one action to other action in the same Controller or different Controllers. Usually, TempData object will be stored in a session object. Tempdata is also required to typecast and for null checking before reading data from it.

Is TempData private to a user?

Yes, Tempdata is private to a user.

Where is TempData stored in MVC?

By default TempData uses the ASP.NET Session as storage. So it is stored on the server ( InProc is the default).

Can we access TempData in view?

TempData is a property in the ControllerBase class. So, it is available in any controller or view in the ASP.NET MVC application. The following example shows how to transfer data from one action method to another using TempData.


2 Answers

For others... ASP.NET MVC 2 has made some changes to TempData. Here is a blog entry with details. In summary:

...The outcome of the changes we made resulted in the following rules that govern how TempData operates:

  1. Items are only removed from TempData at the end of a request if they have been tagged for removal.
  2. Items are only tagged for removal when they are read.
  3. Items may be untagged by calling TempData.Keep(key).
  4. RedirectResult and RedirectToRouteResult always calls TempData.Keep().
like image 74
Donald Byrd Avatar answered Sep 30 '22 05:09

Donald Byrd


According to MSDN, TempData, an instance of TempDataDictionary, is available in classes that derive from ControllerBase, ViewContext, and ViewPage. The data only lasts for a single round-trip: set in one request, removed after the next request.

like image 36
tvanfosson Avatar answered Sep 30 '22 03:09

tvanfosson