Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET TempData isn't cleared even after reading it

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");
    }
}
like image 912
Kiran Vedula Avatar asked Sep 14 '15 18:09

Kiran Vedula


People also ask

Which method ensures that all the items in TempData are not removed at the end of the current request?

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.

Is TempData stored in session?

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.

How do I pass TempData to view?

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.

What is the lifetime of TempData in MVC?

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.

What happens if tempdata is not read on view?

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.

What is tempdata in ASP NET MVC?

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.

How to read tempdata from the next page?

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.

Does read access to temp data remove the item immediately?

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).


1 Answers

Below are some of the key points to note when using Temp data.

  1. A read access to temp data doesn't remove items from the dictionary immediately, but only marks for deletion.

  2. 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)

  3. 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

like image 165
Kiran Vedula Avatar answered Sep 29 '22 17:09

Kiran Vedula