TempData
TempData is also a Dictionary object, which is derived from TempDataDictionary and stores the data in key-value pairs.
TempData can be used in two scenarios:
- Passing Data from Controller to View.
- Passing Data from one Action to another.
Also, unlike ViewBag and ViewData, TempData can persist the value for multiple successive requests. Below are the important points we should keep in mind while using TempData:
- Type Casting for complex types is necessary.
- Checking for null is required.
- Due to boxing and unboxing, unnecessary use of it must be avoided.
- No IntelliSense.
Let's understand TempData with an example:
i) From Controller to View:
public ActionResult Index()
{
Employee emp = new Employee();
emp.Id = 1;
emp.Name = "Michael";
emp.Age = 22;
TempData["Employee"] = emp;
return View();
}
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
ii) From Action to Action:
public ActionResult ActionOne()
{
Employee emp = new Employee();
emp.Id = 1;
emp.Name = "Michael";
emp.Age = 22;
TempData["Employee"] = emp;
return RedirectToAction("TempView");
}
public ActionResult ActionTwo()
{
Employee emp1 = new Employee();
emp1=(Employee)TempData["Employee"];
return View();
}
Now, in the ActionTwo action, TempData can be used to get an Employee object.
@{
ViewBag.Title = "ActionTwo";
}
<h2>ActionTwo</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
TempData is different from ViewBag and ViewData in the sense that it can retain values in successive requests, depending on how the data is being read.
Normal Read
If you do a normal read, the value inside TempData will not be persisted for the next request and will become null.
No Read
The value inside TempData is persisted until it is not read in the view.
Peek Read
If you do a Peek read, it uses the Peek method of TempData to read the value in the view. Also, the value will be persisted.
Read & Keep
If you do a normal read but use the Keep method of TempData to keep the value, the value will be persisted for the next request.
Let's understand the above with examples:
First, we will read the value from TempData normally and will try to find out whether the value is persisted or becomes null for the next request.
So, I have created two Actions, and read the value in the view of the first action itself, so the TemData for the next action should be null.
public ActionResult Index()
{
Employee emp = new Employee();
emp.Id = 1;
emp.Name = "Michael";
emp.Age = 22;
TempData["Employee"] = emp;
return View();
}
public ActionResult TempView()
{
Employee emp1 = new Employee();
emp1=(Employee)TempData["Employee"];
return View();
}
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
<a href="/Student/TempView"> request</a>
@{
ViewBag.Title = "TempView";
}
<h2>TempView</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
On clicking the next request anchor, we will get the null reference exception as the value of TempData["Employee"] has already been read in the Index view, so for the TempView Action, it becomes null. When we try to read it in the TempView's view, we get the expected error.
Similarly, if we do not read the TempData value in the Index view, it will be persisted for TempView Action, and we do not get any error in TempView's view.
Now, let's do a Peek read for the same example and check what happens:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData.Peek("Employee");
}
@emp.Id
@emp.Name
@emp.Age
<a href="/Student/TempView">Next request</a>
@{
ViewBag.Title = "TempView";
}
<h2>TempView</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
Here, the Index view is using the Peek method of TempData to read the value, so the value of TempData is persisted for the next request, and we don't get any null reference error upon clicking the anchor tag. Instead, we get the desired output.
At last, let's modify the above example and do a normal read and use the Keep method of TempData to persist the value for the next request.
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
TempData.Keep("Employee");
}
@emp.Id
@emp.Name
@emp.Age
<a href="/Student/TempView">Next request</a>
@{
ViewBag.Title = "TempView";
}
<h2>TempView</h2>
@using MVCExample.Models
@{
var emp = (Employee)TempData["Employee"];
}
@emp.Id
@emp.Name
@emp.Age
Here, we can also see that the value is persisted for the next request, and on clicking the next request anchor, we get the desired output.