I have a controller action something similar to the below, TempData was initialized by my framework. I've noticed that TempData doesn't clear out the value once it is read as shown in the action "EmployeeUnderAge".
When does TempData clears the data that has been read?
public class HomeController : Controller
{
public ActionResult Index(int iD)
{
var employeeAge = (int)TempData["Age"];
RouteData.Values.Add("Age", employeeAge);
return RedirectToAction("EmployeeUnderAge");
}
public ActionResult EmployeeUnderAge(int employeeAge)
{
var stillInTempData = (employeeAge == ((int) TempData["Age"]));
return (stillInTempData) ? View("Index") : View("Error");
}
}
void Keep(string key) Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.
It is stored in session storage, but there is one crucial difference between TempData and Session : TempData is available only for a user's session, so it persists only till we have read it and gets cleared at the end of an HTTP Request.
Passing the data from Controller to View using TempData To pass the strongly typed data from Controller to View using TempData, we have to make a model class then populate its properties with some data and then pass that data to TempData as Value and selecting Key's name is the programmer's choice.
So it means that the TempData value persists at successive requests and can transmit from a Controller to a View. After transferring a TempData value from a controller to a View, if you again try to use it at another level then it will lose its value and become null.
If the value of TempData is not read, then it will persist that data for the next request. In other words, suppose we are storing the data in TempData, say TempData [“CurrTime”]= DateTime. Now, if we do not read it on View, then for the next request, the value of TempData [“CurrTime”] will remain the same.
ASP.NET MVC - TempData. TempData in ASP.NET MVC can be used to store temporary data which can be used in the subsequent request. TempData will be cleared out after the completion of a subsequent request.
The "read" operation can take place even on the next page or some subsequent next page. The data continues to "stay" untill it is read/extracted. A TempData variable is created in the backing class by marking it with the attribute [TempData]. TempData can be read in the same manner as we read ViewData and Session.
1) A read access to temp data doesn't remove items from the dictionary immediately, but only marks for deletion. 2) Temp data will not always remove the item that has been accessed. It only removes the item when an action results in an Http 200 status code (ViewResult/JsonResult/ContentResult etc).
Below are some of the key points to note when using Temp data.
A read access to temp data doesn't remove items from the dictionary immediately, but only marks for deletion.
TempData will not always remove the item that has been read. It only removes the item when an action results in an HTTP 200 (OK) status code (ie: ViewResult/JsonResult/ContentResult etc)
In case of actions that result in an HTTP 302 (such as any redirect actions), the data is retained in storage even when it is accessed which is the case in my question. TempData apparently is designed for passing data to different controllers/actions and hence doesn't clearing out during redirects is justified
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