How To Use TempData
we use TempData to store the data and we access it in consecutive requests. Internally it is using session. Its type is TempDataDictionary. It automatically clears after subsequent requests and we need to manually clear the session variable. We can use TempData in controller actions as well. We need to cast the TempData and ViewData as well before we consume them in our action or view. And the final suggestion is, only use ViewBag, ViewData, TempData when you really need them. If you can complete your task with other better techniques then go ahead with those techniques, like ViewModel is the alternative option of ViewBag and ViewData.
The value we can add in ViewData is of object type and the key would be string type. Similarly, we deal with TempData as we do ViewData. Let’s start with a simple example.
- public class StudentController : Controller
- {
- // 1st Request
- public ActionResult Index()
- {
- TempData["name"] = "Usama";
- TempData["institute"] = "GCUF";
- return View();
- }
- // 2nd Request
- public ActionResult Details()
- {
- return View();
- }
- }
Now create these action views and use the TempData there.
- @{
- var name = "";
- var institute = "";
- if (TempData.ContainsKey("name"))
- {
- name = TempData["name"] as string;
- }
- if (TempData.ContainsKey("institute"))
- {
- institute = TempData["institute"] as string;
- }
- }
- <h2> @name </h2>
- <h2> @institute </h2>
This is how we use the TempData. It is important to cast the ViewData and TempData variables before use. And we also check the values of these variables to handle the null reference exception as we’re doing here in this example. It automatically clears on the third request because its life is only limited to the subsequent request. Now let’s just add a Url.Action on Index View.
- @Html.ActionLink("Details", "Details", "Student")
Now run the application. You’ll see that TempData is only working in Index view and it is not showing any content in Details view. Actually we’re initializing the TempData variable in Index action and then the consecutive request is Index view where we’re using TempData variables. But then when we redirect to the Details action view, it is not showing anything, because here TempData clears.
Difference of ViewBag, ViewData And TempData
The major difference is we use ViewBag and ViewData to pass the data from controller action to views. But we can’t access these variables in controller actions. But we can use the TempData in our actions as well. Let’s take a look,
Comment the Index view code.
- @{
- ViewBag.Title = "Index";
- }
- @Html.ActionLink("Details", "Details", "Student")
- @*@{
- var name = "";
- var institute = "";
- if (TempData.ContainsKey("name"))
- {
- name = TempData["name"] as string;
- }
- if (TempData.ContainsKey("institute"))
- {
- institute = TempData["institute"] as string;
- }
- }
- <h2> @name </h2>
- <h2> @institute </h2>*@
And now use TempData variables in Details action and use them into the view with ViewBag.
- // 2nd Request
- public ActionResult Details()
- {
- if (TempData.ContainsKey("name"))
- {
- ViewBag.Name = TempData["name"] as string;
- }
- if (TempData.ContainsKey("institute"))
- {
- ViewBag.Institute = TempData["institute"] as string;
- }
- return View();
- }
- @{
- ViewBag.Title = "Details";
- }
- <h2> @ViewBag.Name </h2>
- <h2> @ViewBag.Institute </h2>

This is how we use and get the values from TempData in controller actions as well and it was not possible in ViewBag and ViewData scenarios.
How to Maintain the State of TempData
Now what we want is to maintain the state of TempData on the second request to make the values available on the third request as well. For this purpose, we just need to use the keep function with TempData variables. First of all make the third action in Students controller and copy the code from Details action and paste it into About action.
- public ActionResult About()
- {
- if (TempData.ContainsKey("name"))
- {
- ViewBag.Name = TempData["name"] as string;
- }
- if (TempData.ContainsKey("institute"))
- {
- ViewBag.Institute = TempData["institute"] as string;
- }
- return View();
- }
- public ActionResult Details()
- {
- if (TempData.ContainsKey("name"))
- {
- ViewBag.Name = TempData["name"] as string;
- }
- if (TempData.ContainsKey("institute"))
- {
- ViewBag.Institute = TempData["institute"] as string;
- }
- TempData.Keep();
- return View();
- }
TempData with Complex Types
The plain types are like int, float, and string. These are the built-in datatypes but the datatype which is defined by the developer where we’re using multiple types is inside and it is based on the requirements. This is called complex types like class, and struct. It is not important to mention but to make the concept clear to the reader, I would say we also define the different operations or function that we can manipulate on this type. To come back to the point, don’t worry about the complex types. If you know how to use classes with ViewBag or ViewData then it is 100% exactly the same with TempData.Take a look at one simple example.
- namespace SMPL.Controllers
- {
- public class StudentController : Controller
- {
- // 1st Request
- public ActionResult Index()
- {
- Student std = new Student
- {
- Name = "Muhammad",
- Institute = "GCUF"
- };
- TempData["info"] = std;
- return View();
- }
- }
- public class Student
- {
- public string Name { get; set; }
- public string Institute { get; set; }
- }
- }
- @using SMPL.Controllers
- @{
- ViewBag.Title = "Index";
- }
- <h2> @((TempData["info"] as Student).Institute)</h2>
- <h2> @((TempData["info"] as Student).Name)</h2>
Comments
Post a Comment