Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Persisting Data with TempData

Persisting Data with TempData

01 Apr 2024
Intermediate
194K Views
4 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

TempData persistence: Overview

TempData is used to pass data from the current request to subsequent requests (which means redirecting from one page to another). Its life is very short and lies only till the target view is fully loaded. But you can persist data in TempData by calling the Keep() method. In this MVC Tutorial, we will explore more about TempData persistence which will include data in TempData, putting persistence data in TempData. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

What is TempData?

It is used to pass data from a current request to a subsequent request which means redirecting from one page to another. We can persist data in TempData by calling the method Keep ().In a real-world example, we need to keep the value in the TempDate object after request completion.In such a case, we always have two overloaded methods to retain value after the current request completion.

  • Void Keep()
  • Void Keep(string Key)

Let's get into detail about these two methods.

TempData with Keep method

If we want to keep the value in the TempData object after request completion, you need to call the Keep method within the current action. There are two overloaded Keep methods to retain value after the current request completion.

void Keep()

When calling we are method within the current action ensures that all the items in TempData are holding not removing at the end of the current request. Let's elaborate on this in C# Compiler.

View Code

@model SampleProject.Models.UserModel;

@{
  Layout="~/Views/Shared/_Layout.cshtml";
  ViewBag.Title="TempData Demo";
  Var tempDataUserPersist= (User) TempData["User"];
  TempData.Keep();
} 

Action -Controller Code

 using SampleProject.Models.UserModel;

public ActionResult Index()
{
   Var UserList= db.User.ToList();
   TempData["User"]= UserList;
   TempData.Keep();
   return View();
}

void Keep(string key)

Calling this method within the current action ensures that a specific item in TempData is not removed at the end of the current request.

View Code

 @model SampleProject.Models.UserModel;

@{
  Layout="~/Views/Shared/_Layout.cshtml";
  ViewBag.Title="TempData Demo
  Var tempDataUserPersist= (User) TempData["User"];
  TempData.Keep("User");
}

Action-Controller Code

using SampleProject.Models.UserModel;
public ActionResult Index()
{
   Var UserList= db.User.ToList();
   TempData["User"]= UserList;
   TempData.Keep("User");
   return View();
}  

Accessing the TempData

// Post method 
[HttpPost]
public ActionResult Index()
{
   Var UserPersist=  TempData["User"];
   TempData.Keep("User");
   return View();
}    

The key point about TempData and TempData.Keep()

  • Items in TempData will only tagged for deletion after they have read.
  • Items in TempData can be untagged by calling TempData.Keep(key).
  • RedirectResult and RedirectToRouteResult always call TempData.Keep() to retain items in TempData.
Conclusion:

In this article, you have learned how to persist data in TempData. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification.

FAQs

Its life is very short and lies only till the target view is fully loaded.

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

ViewData and ViewBag offer simplicity for single-request data transfer within a controller and view, while TempData excels in retaining data across a single subsequent request, making it valuable for redirect scenarios.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this