Introduction :
TempData helps to maintain data when we want to transfer data from one action method to another action method of the same or a different controller as well as redirects.
I have already discussed what is TempData and why and when to use TempData in previous article .
Now learn what is difference between TempData keep() and TempData peek().
TempData keep() Mehod Example:
Controller Action Method:
public ActionResult About() { TempData["KeepMessage"]= "TempData keep() Example in MVC"; return View(); }
View :
@TempData[“KeepMessage“]
@{ TempData.Keep(“KeepMessage“); } /
Keep() method marks the specified key in the dictionary for retention
You can use Keep() when prevent/hold the value depends on additional logic.
when you read TempData once and want to hold data for next request then use keep method, so TempData can available for next request as above example.
TempData Peek() Mehod Example:
Controller Action Method:
public ActionResult About() { TempData["peekMessage"] = "TempData Peek() Example in MVC"; return View(); }
View :
@TempData.Peek(“PeekMessage“)
Peek() method returns an object that contains the element that is associated with the specified key, without marking the key for deletion
Using Peek method both the operation of accessing and persisting value is done in a single Call/statement.
Using Tempdata peek method we can perform read and hold data for next request in single call.
Conclusion :
keep method is used when need to hold data for next request, by using peek method we can read and hold data for next request in single call.
Learn More Tutorial
- File(Image) Upload in ASP.NET Core MVC with Example
- CRUD Operation Using Asp.Net Core Mvc with Repository Pattern
- ASP.Net Core: Form Submit (Post) with Example
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
9 Comments