Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • ASP.NET MVC - Get Started
  • MVC Architecture
  • MVC Version History
  • Create First MVC App
  • MVC Folder Structure
  • Routing
  • Controller
  • Action method
  • Action Selectors
  • ActionVerbs
  • Model
  • View
  • Integrate Model, View & Controller
  • Model Binding
  • Create Edit View
  • Razor Syntax
  • Html Helpers
  • Exception Handling
  • Validation
  • Layout View
  • Partial View
  • ViewBag
  • ViewData
  • TempData
  • Filters
  • ActionFilters
  • Bundling
  • ScriptBundle
  • StyleBundle
  • Area
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

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"]

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();
    }
}
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.