Total Page Preview:   000000044478

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

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…
McCranie
26-Aug-2020
This piece of writing will help the internet viewers for building up new weblog or even a weblog from start to end.
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
Suttor
21-Nov-2019
Hi mates, its wonderful post regarding cultureand entirely defined, keep it up all the time.
Wise
12-Aug-2020
This text is worth everyone's attention. Where can I find out more?
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!
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.
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.
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 ;)
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!
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
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?
Lance
22-Oct-2020
Excellent beat ! I wish to apprentice while you amend your web site, how could i subscribe for a blog web site? The account helped me a appropriate deal. I had been a little bit acquainted of this your broadcast offered brilliant clear concept
Makutz
01-Mar-2021
Good post. I certainly appreciate this site. Thanks!
Counts
02-Feb-2021
Many thanks! This is definitely an astonishing webpage!
Gatlin
15-Mar-2021
WOW just what I was searching for. Came here by searching for What is difference between Peek vs Keep or MVC TempData – Peek vs Keep
Nesmith
22-Mar-2021
Simply desire to say your article is as amazing. The clearness for your post is simply spectacular and i could think you are a professional on this subject. Fine along with your permission let me to take hold of your feed to keep updated with imminent post. Thanks one million and please keep up the rewarding work.
Baddeley
21-Jul-2021
Hookup Girls Utilizes Free of charge Matters? A Fantastic Side to side Reward! Free hookup ladies chat hookups on the web is the solution if you're tired of going to pubs and organizations just to be prevented, and even worse, laughed at. I understand what it's like because I've been there. I had been solitary and needy back in the day -- I needed a new lover -- but I continued attempting because I had hardly any other choice. If you're one particular gentleman who wants to hookup with alluring women without likely to those locations where ladies are alone, then this post may just make positive changes to lifestyle. It can clarify why dating on the web is the perfect substitute if you're a guy who may be shy to method a beautiful girl in a nightclub or club.
Rotton
13-Jun-2021
Hi there! Someone in my Myspace group shared this site with us so I came to give it a look. I'm definitely loving the information. I'm book-marking and will be tweeting this to my followers! Fantastic blog and wonderful style and design.

                           
                           

                           

                           

Facebook User: