I'd like to know what the scope and visibility of TempData is in ASP.NET 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.
Yes, Tempdata is private to a user.
By default TempData uses the ASP.NET Session as storage. So it is stored on the server ( InProc is the default).
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.
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:
TempData
at the end of a request if they have been tagged for removal.TempData.Keep(key)
.RedirectResult
and RedirectToRouteResult
always calls
TempData.Keep()
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With