Join Class

TempData in MVC Application

TempData is another dictionary object for storing data using key-value, in this post we learn how to use TempData in MVC application.

TempData is a dictionary object derived from TempDataDictionary, which can contain key-value pairs, useful for transferring data from controller to view in ASP.NET MVC Application, TempData stays for a subsequent HTTP Request as opposed to other options ( ViewBag and Viewdata ) those stay only for current request.

So "TempData value stays for a subsequent HTTP Request" what does that mean?
Here we have created a small form with simple submit button below, which says "Transfer TempData".

How to use TempData in Asp.Net MVC?

On click of that submit button the form will be redirected to a different page (ActionResult submitTempdata), there we will be able to access the @TempData["tempdata"] value.

public ActionResult submitTempdata()
    TempData["tempdata"] = string.Format("This tempData string generated at {0}", DateTime.Now);
    return RedirectToAction("viewbag-tempdata");
Click this button to test TempData =>
C# Syntax of TempData
public TempDataDictionary TempData { get; set; }

You can add key-value pair to TempData, here is an example of how you can write TempData at Controller

TempData["PageTtitle"]="I am learning TempData at WebTrainingRoom";

Now to retrieve the property at View(razor)

You also can add any custom object, list, array in TempData, and cast them back in right data type in View. here is an example.

public ActionResult actionTempata()
ArrayList alColors = new ArrayList();
alColors.Add("Orange"); alColors.Add("Blue");
TempData["Colors"] = alColors;
    return View(); 

Now in View (razor) you can access the TempData property this way.

    ArrayList _colors = TempData["Colors"] as ArrayList;
    foreach (string color in _colors)

Ideal use of TempData when you have small amount of dictionary data (with key-value pair) to be transferred from current page to a different page (view).

Things to remember when using TempData
  • TempData does not use the same dictionary as ViewBag and ViewData, so in one action (page) if you are using TempData, ViewData with same key name, doesnot matter, TempData value will remain same.
    for example, if you write in your controller
    TempData["student"] = "Anushka";
    ViewData["student"] = "Anisha";

    then access the TempData @TempData["student"] ,this will print "Anushka" only, not "Anisha", because they use different dictionary

  • TempData can transfer value from current page to another page
  • There is no intellisense that will get you the key name you have specified in TempData, So you have to remember the exact name while accessing it, also the type,

    for example ArrayList _colors = TempData["Colors"] as ArrayList;

    we have converted to ArrayList, so you have to remember both key name and type

You may also learn about ViewBag and Viewdata in MVC, or read difference among ViewBag, ViewData and Tempdata

Use TempData in MVC
Aspnet MVC Training MVC tutorials, learn model view controllers with c#, develop database driven web application using MVC framework.
Hire .Net Developer
Free Tutorials
ASP.NET MVC Interview Questions Answers
Asp.Net MVC C# Examples | Join Asp.Net MVC Course | Core Tutorial