TempData in MVC

by Sachin Singh


Posted on Tuesday, 07 April 2020

Tags: what is tempdata in mvc when do we use peek method of tempdata when do we use keep method of tempdata in mvc Temp.Keep() Temp.Peak()

TempData:-

TempData is also a dictionary object, which is derived from TempDataDictionary and stores the data in Key-value pair.

TempData can be used in two scenario.
  1. Passing Data from Controller to View.
  2. 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.
  1. Type Casting For complex type is necessary.
  2. Checking for null is required.
  3. Due to boxing and unboxing, unnecessary use of it must be avoided.
  4. No intellisence.

Lets understand TempData with 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 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 request, depending on how the data is being read.

• Normal Read:-

If you do a normal read ,then value inside TempData will not be persisted for the next request and will become null.

• No Read:-

The Value inside TempData is Persisted untill it is not read in the view.

• Peek Read:-

If you do a Peek read meaning uses the Peek method of TempData to read the value in view then also the value will be persisted.

• Read &Keep:-

If you do a normal read but uses keep Method of TempData to keep the value then also the value will be persisted for next request.

Let's understand the above with examples:-

First we will read the value from TemData normally and will try to find out whether the value is persisted or becomes null for the next request.

So, I have Create two Actions ,and Read the value in the view of 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 Index view,so for TempView Action it becomes null and 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 Index view then 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 next request and we don't get any null reference error on clicking anchor tag instead we get the desired output.

At last ,Let's modify the above example and do a normal read and use Keep method of TempData to persist the value for 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,also we can see that value is persisted for the next request and on clicking the next request anchor we get the desired output.