All About the TempData in MVC

This article explains TempData, a dictionary object to store data temporarily.

Introduction

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.

Example

public ActionResult FirstRequest()
{
    List<string> TempDataTest = new List<string>();
    TempDataTest.Add("Tejas");
    TempDataTest.Add("Jignesh");
    TempDataTest.Add("Rakesh");
     TempData["EmpName"] = TempDataTest;
    return View();
}

public ActionResult ConsecutiveRequest()
{
    List<string> modelData = TempData["EmpName"] as List<string>;
    TempData.Keep();
    return View(modelData);
}

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).
 
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

Conclusion

This article helps to learn all about the TempData.