All About the TempData in MVC


TempData is a dictionary object to store data temporarily. It is a TempDataDictionary class type and instance property of the Controller base class.

TempData is able to keep data for the duration of a HTP request, in other words it can keep live data between two consecutive HTTP requests. It will help us to pass the state between action methods. TempData only works with the current and subsequent request. TempData uses a session variable to store the data. TempData Requires type casting when used to retrieve data.

TempDataDictionary is inherited from the IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>> and IEnumerable interfaces.


  1. public ActionResult FirstRequest()  
  2. {  
  3.     List<string> TempDataTest = new List<string>();  
  4.     TempDataTest.Add("Tejas");  
  5.     TempDataTest.Add("Jignesh");  
  6.     TempDataTest.Add("Rakesh");  
  7.      TempData["EmpName"] = TempDataTest;  
  8.     return View();  
  9. }  
  11. public ActionResult ConsecutiveRequest()  
  12. {  
  13.     List<string> modelData = TempData["EmpName"as List<string>;  
  14.     TempData.Keep();  
  15.     return View(modelData);  
  16. } 

Where TempData is stored

By default TempData is stored inside the session variable. Most of the time probably we do not think about where the TempData is stored. Obviously it doesn't matter for the developer. However what happens when someone decides to change the Session State mode from InProc (default session state mode) to any outProc mode and we stored our complex mode into the TempData? Obviously our application will throw an Exception. So these modes require all objects stored in the session to be serializable.

ITempDataProvider Interface defines the contract for the TempData Provider. It stores the data viewed in the next request. It simply contains the method of saving and loading the TempData dictionary.

TempData stored

The SessionStateTempDataProvider class is inherited from the interface ITempDataProvider. SessionStateTempDataProvider provides session state data to the current TempDataDictionary object.
TempDataDictionary object

LoadTempData method definition
LoadTempData method definition

SaveTempData method definition
SaveTempData method definition

Looking into the MVC Page life cycle, after creating the request handler, the framework creates a controller class using the Execute method. Internally this method calls the Controller's ExecuteCore method. It loads TempData and then executes the current action and saves the new dictionary. After release the MVC 5.0 ExecuteCore Method becomes asynchronous. This method calls the PossiblyLoadTempData method to load the TempData. Internally the PossiblyLoadTempData method calls the load method of TempDataDictionary (in other words TempData).

TempDataDictionary has two methods, Load and Save; the Load method loads the specified controller context using the specified data provider. The Save method saves the specified controller context using the specified data provider.

TempData Property of Controller Base Class
TempData Property of Controller Base Class


This article helps to learn all about the TempData.