w3resource

TempData -ASP.NET MVC


In the previous tutorial, we explored ways of moving data from the controller to the views: ViewBag and ViewData. They both did the job, but one major concern about them is that they lose their data on redirect.

In this tutorial, we will explore TempData a dynamic container used to transfer bits of data from the controller to the views, which even on redirect does not lose the data saved in them.

TempData in ASP.NET MVC can be used to store temporary data which can be used in the subsequent request. TempData will only be cleared out after the completion of a subsequent request.

TempData is useful when you want to transfer non-sensitive data from one action method to another action method of the same or a different controller as well as redirects.

Adding Values to TempData

To add values to TempData, you can add a key-value pair temporary data to TempData as shown in the code snippet below:

public class HomeController: Controller
{
    // GET: Activity
    public HomeController()
    {
return View();
    }
    public ActionResult Index()
    {
        TempData["ActivityName"] = "Write Tutorials on C#";
        TempData["TimeTaken"] = 30;
        return View();
    }



    public ActionResult About()
    {
        String ActivityName;
        int TimeTaken;

        if(TempData.ContainsKey("ActivityName"))
            ActivityName = TempData["name"].ToString();
    
        if(TempData.ContainsKey("TimeTaken"))
            TimeTaken = int.Parse(TempData["TimeTaken "].ToString());
    
        // do something with ActivityName or TimeTaken here 
        return View();
    }
}

In the above example, we have added data into TempData and accessed the same data using it's key inside another action method. Please notice that we have converted values into the appropriate type.

To better understand the data life cycle of the information stored in TempData, take a look at the screenshot below:

asp.net data life cycle of the information

As you can see in the above example, we add test data in TempData in the first request and in the second subsequent request we access test data from TempData which we stored in the first request. However, you can't get the same data in the third request because TempData will be cleared out after second request.

The TempData.Keep() method can be called to retain TempData values in a third consecutive request.

//Example: TempData.Keep()
public class HomeController : Controller
{
    public HomeController()
    {
        return View();
    }
    public ActionResult Index()
    {
        TempData["Name"] = "Ogbonna Vitalis";
        return View();
    }

    public ActionResult About()
    {
        string Name;
        if(TempData["Name"] != null)
            Name = TempData["myData"].ToString();
        TempData.Keep();
        return View();
    }

    public ActionResult Blog()
    {
        string Name;
        if(TempData["Name"] != null)
            Name = TempData["Name"] as string;
            
        return View();
    }
}

The diagam below illustrates the effect of calling the TempData.keep() function in the code snippet above

asp.net TempData values in a third consecutive request