Total Page Preview: 000000048025
MVC TempData – Peek vs Keep
Most of the MVC developer knows that TempData is used to preserve data for a single request but reality is that TempData can pass data for next request also. In this article we will discuss how to persist data with TempData using Keep and Peek method as well we will also see the difference between Keep and Peek method.
TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary.TempData used to transfer data between controllers or between actions. There is one point to note that TempData is only work during the current and subsequent request and it is generally used to store one time message.
But one can persist data in TempData object even after request completion with the help of Keep() or Peek() method.
-
Condition 1 – Not Read in First Request (Not Read).
-
Condition 2 – Read In First Request (Normal Read).
-
Condition 3 – Read & persist using Keep (Read and Keep).
-
Condition 4 – Persist using Peek and Read (Read and Peek).
Let us discuss this one by one in detail:
1. Not Read in First Request : If we do not read “TempData” in the current request then “TempData” value will be persisted for the next request.
2.Read In First Request : If we read “TempData” in the current request then “TempData” value will be not persist for the next request.
On View:
@TempData["key"]
Store in variable:
string message = TempData["key"];
3.Read & Persist using Keep : If we read “TempData” in the current request and we can keep method to persist TempData for the next request. In MVC, we are having void keep() and void keep(string key) methods to persist the data.
1. void Keep()
This method you can use when all items in TempData you want to retain and does not allow deletion for any TempData’s items.
Example:
@TempData["key"];
TempData.Keep();
2. void Keep(string key)
This method you can use when particular TempData’s value you want to persist and does not allow deletion for that particular TempData’s value. Rest of the TempData’s values will be deleted.
Example :
@TempData["key "];
TempData.Keep("key ");
You can understand it by this way, by calling "Keep" method, you are specifying that keep (persist) this TempData for next request.
4.Peek and Read:
If you set a TempData in your action method and if you read it in your view by calling "Peek" method then TempData will be persisted and will be available in next request.
string message = TempData.Peek("Message").ToString();
Peek method is doing both tasks here in same statement: reading and persisting.
Keep and Peek Diagram:
Difference Bitween Keep and Peek :
|
Keep() Method |
Peek() Method |
1 |
To read and retain the value with Keep one need to do two request, i.e. first read the value and in next statement call Keep method to retain value. |
With the help of Peek method one can do both operation in a single statement i.e. access as well retain value.
|
2 |
Keep provides 2 overload methods. One can save particular TempData on condition based and second can save all TempData’s value.
|
There is no overloaded method in case of Peek method. Peek method always saves particular TempaData’s value.
|
3 |
RedirectResult and RedirectToRouteResult internally calls Keep method to retain items.
|
Peek method is not called internally with any of ActionResult.
|
4 |
Once we retrieved value from object than it is marked for deletion, with Keep method we can later save object from Deletion. It means first deletion marking is happened then saving using Keep method.
|
With Peek method we can retain TempData value without marking for deletion in a single call. It means deletion marking is not happening in case of Peek method. It directly persist TempData.
|
Thank You
About Author
Brijesh Kumar
Database Developer
I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL
Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall
and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com