11
OctPersisting Data with TempData
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.