What is TempData?
TempData is a mechanism used to pass a small amount of temporary data from a controller action method to a view or another action method, either within the same controller or in a different controller. By default, TempData values are cleared once the subsequent request is completed. However, this default behavior can be modified if needed. If you examine the Controller class definition, you will find the following signature for the TempData property.
public TempDataDictionary TempData {get;set;}
As you can see the TempData property returns a TempDataDictionary
public class TempDataDictionary : IDictionary<string, object>
Update the EmployeeController as shown below.
The TempDataDictionary class implements the IDictionary interface, meaning TempData in ASP.NET MVC functions as a dictionary object. As a dictionary, it stores data in key-value pairs, where the key is always a string, and the associated value is stored as an object type.
namespace FirstMVCDemo.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Method1()
{
TempData["Name"] = "sana";
TempData["Age"] = 30;
return View();
}
public ActionResult Method2()
{
string Name;
int Age;
if (TempData.ContainsKey("Name"))
Name = TempData["Name"].ToString();
if (TempData.ContainsKey("Age"))
Age = int.Parse(TempData["Age"].ToString());
// do something with user Name or Age here
return View();
}
public ActionResult Method3()
{
string Name;
int Age;
if (TempData.ContainsKey("Name"))
Name = TempData["Name"].ToString();
if (TempData.ContainsKey("Age"))
Age = int.Parse(TempData["Age"].ToString());
// do something with userName or userAge here
return View();
}
}
}
In the example above, we stored data in TempData and retrieved it using a key in a different action method. Note that we converted the values to the appropriate type when accessing them.
To preserve the TempData value for a third consecutive request, we need to use the TempData.Keep() method. Let's explore its usage with an example.
namespace FirstMVCDemo.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Method1()
{
TempData["Name"] = "sana";
TempData["Age"] = 30;
return View();
}
public ActionResult Method2()
{
string Name;
int Age;
if (TempData.ContainsKey("Name"))
Name = TempData["Name"].ToString();
if (TempData.ContainsKey("Age"))
Age = int.Parse(TempData["Age"].ToString());
TempData.Keep();
// do something with userName or userAge here
return View();
}
public ActionResult Method3()
{
string Name;
int Age;
if (TempData.ContainsKey("Name"))
Name = TempData["Name"].ToString();
if (TempData.ContainsKey("Age"))
Age = int.Parse(TempData["Age"].ToString());
// do something with userName or userAge here
return View();
}
}
}
We have created a ViewModel class named EmployeeDetailsViewModel. In this name, "Employee" represents the controller, "Details" corresponds to the action method, and "ViewModel" is prefixed to indicate that it is a ViewModel. While following this naming convention is not mandatory, I personally find it a good practice.