TempData is a dictionary that is used to store data that needs to be preserved between requests. By default, TempData is cleared after each request. However, there are a few ways to keep data in TempData for the next request.
Use the Keep() method: The Keep() method tells TempData to keep the specified key-value pair for the next request. For example, the following code will keep the value "foo" in TempData for the next request:
Code snippet
TempData.Keep("foo");
Use the Peek() method: The Peek() method returns the value for the specified key without removing it from TempData. This means that the value will still be available for the next request. For example, the following code will get the value of "foo" from TempData without removing it:
Code snippet
string foo = TempData.Peek("foo");
Use the Session object: The Session object is a collection that is used to store data that needs to be preserved between sessions. By default, Session is cleared after each browser is closed. However, you can configure Session to be persistent, which means that it will be preserved between browser restarts.
It is important to note that TempData is not a permanent storage mechanism. It is designed to store data for a short period of time, such as the duration of a single user session. If you need to store data for a longer period of time, you should use a database or a file system.
The “TempData” is available throughout for the current request and in the subsequent request, it’s available depending on whether “TempData” is read or not.
So if “TempData” is once read it will not be available in the subsequent request.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
TempData is a dictionary that is used to store data that needs to be preserved between requests. By default, TempData is cleared after each request. However, there are a few ways to keep data in TempData for the next request.
Code snippet
Code snippet
It is important to note that TempData is not a permanent storage mechanism. It is designed to store data for a short period of time, such as the duration of a single user session. If you need to store data for a longer period of time, you should use a database or a file system.
The “TempData” is available throughout for the current request and in the subsequent request, it’s available depending on whether “TempData” is read or not.
So if “TempData” is once read it will not be available in the subsequent request.