Persisting Data with TempData

Persisting Data with TempData

01 Apr 2024
Intermediate
190K Views
4 min read

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

Q1. How long does TempData persist?

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

Q2. What is the purpose of the TempData?

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.

Q3. What is difference between TempData and ViewData?

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

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 14 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core (Project) Jun 22 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this