Please describe me briefly about TempData in MVC with best example and with image also.
Define TempData in MVC.
32327-Jan-2020
Updated on 27-Jan-2020
Home / DeveloperSection / Forums / Define TempData in MVC.
Please describe me briefly about TempData in MVC with best example and with image also.
Nishi Tiwari
27-Jan-2020ASP.NET MVC TempData
TempData is also a dictionary derived from TempDataDictionary class and stored for short lives session and it uses a string key and object value and the difference is in their life cycle of the object. TempData mainly keeps the information for the time of an HTTP Request. Which mean only from one page to another. TempData works with a 302/303 redirection because it is within the same HTTP Request. It also helps to maintain data when we move from one controller to other controller or also from one action to the other action. In other words, when we redirect, then it helps to maintain data between those redirects. It mainly internally uses session variables. Temp data mainly use during the current and subsequent request only means it is used when we are sure that next request will be redirecting to next view. It also requires typecasting for the complex data type and check for null values to avoid any error. It is used mainly to store only one time messages like error messages, validation messages.
TempData represents a set of data which persists only from one request to the next. It is derived from TempDataDictionary, we can also use its object to pass data as we did in ViewData. The value of TempData persists only from one request to the next as above mentioned. Retention is also used to mark key to persist data so that it may retain for the next request.
We can also use TempData to pass data from one action to another action.
Example
Controller
View
Output:
This index file produces the following output to the browser.