4

I've been working with TempData lately and facing a confusing case:

Supposing that the TempData is created in the following Action:

public ActionResult MyAction1()
{
  //...
  myTempData = TempData["myTempData"];
  //..
}

and is expected to be use in the following Action:

public ActionResult MyAction2()
{
  //...
  TempData["myTempData"] = myTempData;
  //..
}

I understand that if I call MyAction2 on the next request, the TempData value will be deleted. But if I call other action, not MyAction2, on the next request, will TempData be deleted? If it will be, is there any trick to make sure it exist until the end of the session?

Thanks all.

Chanh Tran
  • 231
  • 2
  • 8
  • Have you tried it to find out? It is my understanding that this uses session to store the data and is wiped out when the data is read, unless it is read with .Peek – Slicksim Jul 14 '17 at 06:59
  • It will be available in subsequent request (redirection), no matter which `action method` you are about to call. – mmushtaq Jul 14 '17 at 07:04
  • @Slicksim I think so, too but not quite sure :D – Chanh Tran Jul 14 '17 at 07:19

2 Answers2

9

Here you go for Keep and Peek Temp Data For next Request :

If you won't read Temp Data then it will be available for next subsequent request

So let’s discuss these four conditions in more detail

“Tempdata helps to preserve values for a single request”.

The other half-truth which developers do not know is or I will say which confuses developer is:

“TempData CAN ALSO preserve values for the next request depending on 4 conditions”..

  1. Not Read
  2. Normal Read
  3. Read and Keep
  4. Peek and Read

Condition 1 (Not read): If you set a “TempData” inside your action and if you do not read it in your view, then “TempData” will be persisted for the next request.

Condition 2 (Normal Read): If you read the “TempData” normally like the below code, it will not persist for the next request.

   stringstr = TempData["MyData"];

Even if you are displaying, it’s a normal read like the code below:

   @TempData["MyData"];

Condition 3 (Read and Keep): If you read the “TempData” and call the “Keep” method, it will be persisted.

   @TempData["MyData"];
   TempData.Keep("MyData");

Condition 4 ( Peek and Read): If you read “TempData” by using the “Peek” method, it will persist for the next request.

   stringstr = TempData.Peek("Td").ToString();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Laxman Gite
  • 2,248
  • 2
  • 15
  • 23
  • So it's like Request 1 I create the `TempData`, Request 2 3 4 I do not read it but call other Action instead, then on Request 5 the `TempData` is still there? – Chanh Tran Jul 14 '17 at 07:13
1
TempData["myTempData"]

1) TempData persistence only on action to action, suppose action1 calls you have store the data into TempData["myTempData"] then you need the access data in action2 that will definity persist.

2) if you want to store the data in TempData["myTempData"] then on each and every action first assign the value of TempData["myTempData"] to TempData["myTempData"] then use it in every action until you will removed it forcefully.

Hopefully query will resolved.

Shahzad Khan
  • 432
  • 2
  • 14