I've been using TempData to preserve my model in between page requests, as otherwise I lose access to specific properties of classes in the model that I need.
(It's a slight digression and simplification, but basically I have a List of objects which have a .name and a .active property. I have a CheckBoxFor the .active property, and that is preserved, but the .name property is set to null when the form is submitted; I need access to that property as well, so I've been storing the old model in TempData and then copying over that property when I need it. Is there a better way of doing that? I was quite surprised that properties of objects that I'm using would be nulled.)
The model is stored in TempData when the user checks a checkbox, thus submitting the form. It is read out of TempData at some later time, when the user clicks a button. (There are no intervening requests.)
Is using TempData in such a way safe for multiple users? In other words, does each client get its own copy of TempData? I'm worried about a situation roughly like the following:
What I am unsure about is which model User 1 will get. Could someone please enilghten me?
Yes, TempData is backed by session storage, so if you are in a load balanced environment extra care must be taken when using it (sticky sessions, persistent session state, etc). TempData has been the de-facto choice when using the PRG pattern, and is what it was designed for.
Tempdata helps to preserve values for a single request and CAN ALSO preserve values for the next request depending on 4 conditions”. You can use Peek when you always want to retain the value for another request.
Yes, Tempdata is private to a user.
TempData is session bound. User 1 will only see its TempData.
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