ASP.NET MVC - TempData

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller.

TempData stores the data temporarily and automatically removes it after retrieving a value.

TempData is a property in the ControllerBase class. So, it is available in any controller or view in the ASP.NET MVC application.

The following example shows how to transfer data from one action method to another using TempData.

Example: TempData
public class HomeController : Controller
{
    public ActionResult Index()
    {
        TempData["name"] = "Bill";

        return View();
    }

    public ActionResult About()
    {
        string name;
        
        if(TempData.ContainsKey("name"))
            name = TempData["name"].ToString(); // returns "Bill" 

        return View();
    }

    public ActionResult Contact()
    {
        //the following throws exception as TempData["name"] is null 
        //because we already accessed it in the About() action method
        //name = TempData["name"].ToString(); 

        return View();
    }
}

In the above example, we added data in the TempData in the Index() action method and access it in the About() action method. Assume that the user will go to the Index page first and then to the About page.

The following transfers data from an action method to a view.

Example: TempData
public class HomeController : Controller
{
    public ActionResult Index()
    {
        TempData["name"] = "Bill";

        return View();
    }

    public ActionResult About()
    {
        //the following throws exception as TempData["name"] is null 
        //because we already accessed it in the Index.cshtml view
        //name = TempData["name"].ToString(); 

        return View();
    }

    public ActionResult Contact()
    {
        //the following throws exception as TempData["name"] is null 
        //because we already accessed it in the Index.cshtml view
        //name = TempData["name"].ToString(); 

        return View();
    }
}

Above, we added data in the TempData in the Index() action method. So, we can access it in the Index.cshtml view, as shown below. Because we have accessed it in the index view first, we cannot access it anywhere else.

Index.cshtml
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@TempData["name"]

ADVERTISEMENT

You can also transfer data from a view to controller, as shown below.

Index.cshtml
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    TempData["name"] = "Steve";
}

The above TempData can be accessed in the controller, as shown below.

Example: TempData
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        if(TempData.ContainsKey("name"))
            name = TempData["name"].ToString(); // returns "Bill" 

        return View();
    }

    public ActionResult Contact()
    {
        //the following throws exception as TempData["name"] is null 
        //because we already accessed it in the About() action method
        //name = TempData["name"].ToString(); 

        return View();
    }
}

Although, TempData removes a key-value once accessed, you can still keep it for the subsequent request by calling TempData.Keep() method.

The following example shows how to retain TempData value for the subsequent requests even after accessing it.

Example: TempData.Keep()
public class HomeController : Controller
{
    public ActionResult Index()
    {
        TempData["name"] = "Bill";
        return View();
    }

    public ActionResult About()
    {
        string name;
        
        if(TempData.ContainsKey("name"))
            name = TempData["name"] as string;
        
        TempData.Keep("name"); // Marks the specified key in the TempData for retention.
        
        //TempData.Keep(); // Marks all keys in the TempData for retention

        return View();
    }

    public ActionResult Contact()
    {
        string name;
        
        if(TempData.ContainsKey("name"))
            data = TempData["name"] as string;
            
        return View();
    }
}
Want to check how much you know ASP.NET MVC?