TempData becomes null after refresh the page
Asked Answered
D

1

10

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,

Duodenary answered 25/6, 2012 at 17:25 Comment(0)
T
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.

Throaty answered 25/6, 2012 at 17:31 Comment(9)
I have in the controller in which i consume value of TempData: TempData["data"]="consumerId"; how can i use Keep(), thanks DarinDuodenary
The 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.Throaty
excuse me its like this string consumerId = (string)TempData["data"];Duodenary
Yeap, it's like this. It is inside this controller action that you need to call the Keep method.Throaty
Keep() doesn't existe I'm adding reference Systeme.web.mvc but nothingDuodenary
Inside the controller action you call: TempData.Keep();.Throaty
Like this: System.Web.Mvc.TempDataDictionary.Keep(consumerId );Duodenary
NO! 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();.Throaty
You can also use Keep to retain a single Key of a tempdata with .Keep(Key)Koumis

© 2022 - 2024 — McMap. All rights reserved.