Total Page Preview:   000000039095

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:
 
Abstraction and Encapsulation
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



Comments

Taubman
31-Jan-2017
If you are going for most excellent contents like myself, simply pay a quick visit this site daily since it offers feature contents, thanks
Gore
09-Feb-2017
Hello everyone, it's my first pay a quick visit at this web page, and paragraph is truly fruitful in favor of me, keep up posting these posts.
Lawanya kumar
14-Mar-2019
Thanku so much sir really it's very helpful and several time asking in interview
Lawanya kumar
14-Mar-2019
Thanku so much sir really it's very helpful and several time asking in interview
Lawanya kumar
14-Mar-2019
Thanku so much sir really it's very helpful and several time asking in interview
Butcher
14-Feb-2017
I need to to thank you for this excellent read!! I definitely enjoyed every bit of it. I've got you book-marked to check out new stuff you post…
Emmons
12-Nov-2019
My partner and I stumbled over here from a different web page and thought I may as well check things out. I like what I see so now i am following you. Look forward to looking over your web page again.
Ramsden
13-Nov-2019
It's appropriate time to make some plans for the longer term and it's time to be happy. I have read this put up and if I may just I wish to recommend you some interesting issues or tips. Maybe you can write next articles relating to this article. I wish to read even more things about it!
Rahman
16-Nov-2019
Terrific post however I was wanting to know if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit more. Appreciate it!
Kruttschnitt
16-Nov-2019
I would like to thank you for the efforts you have put in penning this blog. I really hope to check out the same high-grade content from you in the future as well. In fact, your creative writing abilities has motivated me to get my own blog now ;)
Suttor
21-Nov-2019
Hi mates, its wonderful post regarding cultureand entirely defined, keep it up all the time.
Minns
21-Nov-2019
I do not know whether it's just me or if perhaps everyone else encountering issues with your website. It appears like some of the written text within your content are running off the screen. Can someone else please comment and let me know if this is happening to them too? This may be a problem with my browser because I've had this happen previously. Kudos
Gerrard
21-Nov-2019
Yes! Finally something about quest bars cheap.
Keck
22-Nov-2019
Hello all, here every person is sharing these kinds of know-how, so it's nice to read this webpage, and I used to go to see this web site every day.
Nation
26-Jun-2020
Heya! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no data backup. Do you have any methods to stop hackers?
Gadsden
26-Jun-2020
Unquestionably believe that which you said. Your favorite reason seemed to be on the net the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly don't know about. You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

                           
                           

                           

                           

Facebook User: