I used TempData to pass data from an action to another,but when i refresh the page the value of TempData becomes null, how I can solve this probleme? Thanks,
Asked
Active
Viewed 9,473 times
1 Answers
17
Use Session
instead of TempData
. TempData is supposed to be used only for a single redirect. Another possibility is to call the Keep method inside the controller action in which you are consuming the value from TempData. This way if the user refreshes the page by hitting F5 TempData will be persisted for one more request.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
I have in the controller in which i consume value of TempData: TempData["data"]="consumerId"; how can i use Keep(), thanks Darin – Victor Jun 25 '12 at 18:01
-
1The example you have shown is writing to TempData, not reading (consuming). You need to call the Keep method inside the controller action in which you use TempData's value. – Darin Dimitrov Jun 25 '12 at 18:03
-
excuse me its like this string consumerId = (string)TempData["data"]; – Victor Jun 25 '12 at 18:08
-
Yeap, it's like this. It is inside this controller action that you need to call the Keep method. – Darin Dimitrov Jun 25 '12 at 18:09
-
Keep() doesn't existe I'm adding reference Systeme.web.mvc but nothing – Victor Jun 25 '12 at 18:20
-
Inside the controller action you call: `TempData.Keep();`. – Darin Dimitrov Jun 25 '12 at 18:22
-
Like this: System.Web.Mvc.TempDataDictionary.Keep(consumerId ); – Victor Jun 25 '12 at 18:22
-
1NO! `Keep` is an instance method as shown in the documentation I have linked to. This means that you need to call it on an instance of `TempDataDictionary`. Inside the controller action you write `TempData.Keep();`. – Darin Dimitrov Jun 25 '12 at 18:23
-
You can also use Keep to retain a single Key of a tempdata with .Keep(Key) – theLaw Feb 06 '14 at 08:59