Click here to Skip to main content
14,236,142 members

MVC Tempdata, Peek and Keep Confusion

Rate this:
4.90 (56 votes)
Please Sign up or sign in to vote.
4.90 (56 votes)
7 Oct 2014CPOL
This article talks about MVC Tempdata and how MVC tempdata, Peek and Keep works using example and step by step video tutorial on MVC.

This blog assumes you have an idea of MVC. If not, I would suggest starting from this youtube video Learn MVC.

Recently, I was taking MVC class in Mumbai and I saw there was a lot of confusion among participants on how MVC tempdata, Peek and Keep works. I think the confusion stems because most of the MVC developers know only the half truth.

So the half thing which most of the MVC developers know is:

“Tempdata helps to preserve values for a single request”.

The other half-truth which developers do not know is or I will say which confuses developer is:

“TempData CAN ALSO preserve values for the next request depending on 4 conditions”.

So let us try to understand the above two statements. When an end user sends a request to an MVC application, “TempData” is maintained throughout the complete request. This request can traverse through multiple actions or controllers until it displays the view on the browser.

Now in the same session (without closing the browser), if a new / second request is initiated, then “TempData” will be persisted depending on 4 CONDITIONS:

  • Not Read
  • Normal Read
  • Read and Keep
  • Peek and Read

So let’s discuss these four conditions in more detail (Do see the below diagram for better understanding):

Condition 1 (Not read): If you set a “TempData” inside your action and if you do not read it in your view, then “TempData” will be persisted for the next request.

Condition 2 (Normal Read): If you read the “TempData” normally like the below code, it will not persist for the next request.

stringstr = TempData["MyData"];

Even if you are displaying, it’s a normal read like the code below:

@TempData["MyData"];

Condition 3 (Read and Keep): If you read the “TempData” and call the “Keep” method, it will be persisted.

@TempData["MyData"];
TempData.Keep("MyData");

Condition 4 ( Peek and Read): If you read “TempData” by using the “Peek” method, it will persist for the next request.

stringstr = TempData.Peek("Td").ToString();

The above image i have taken from http://stepbystepschools.net/?p=1810

So if you register these four conditions in your mind, you should not have any confusion around TempData :).

Below is a video which demonstrates Tempdata, Keep and Peek.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
QuestionWhat is the difference between peek and keep? Pin
essam kanadily31-Jan-17 22:34
memberessam kanadily31-Jan-17 22:34 
Questionconfusion in peek Pin
Member 782606617-Mar-16 5:39
memberMember 782606617-Mar-16 5:39 
GeneralMy vote of 5 Pin
Member 114610007-May-15 0:16
memberMember 114610007-May-15 0:16 
QuestionHow long keep take my Tempdata. Is it will be removed after session expire in asp.net mvc ? Pin
anirugu20-Feb-15 22:56
memberanirugu20-Feb-15 22:56 
GeneralNice Example Pin
AbinayaAbi23-Nov-14 23:10
memberAbinayaAbi23-Nov-14 23:10 
GeneralMy vote of 5 Pin
robert204810-Nov-14 4:41
memberrobert204810-Nov-14 4:41 
GeneralExcellent stuff Pin
Gaurav Aroraa27-Oct-14 9:19
professionalGaurav Aroraa27-Oct-14 9:19 
QuestionNice and Effective Pin
Suraj Sahoo | Coding Passion7-Oct-14 20:41
professionalSuraj Sahoo | Coding Passion7-Oct-14 20:41 
GeneralThank you Pin
Francis S Michael3-Oct-14 3:12
professionalFrancis S Michael3-Oct-14 3:12 
GeneralNice Explain Pin
aarif moh shaikh2-Oct-14 21:19
professionalaarif moh shaikh2-Oct-14 21:19 
Generalvery good explanation Pin
Afazal MD 310420919-Sep-14 1:20
professionalAfazal MD 310420919-Sep-14 1:20 
GeneralCleared my doubt Pin
Mayank Vashishtha15-Sep-14 20:19
professionalMayank Vashishtha15-Sep-14 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 13 Sep 2014

Tagged as

Stats

152.3K views
25 bookmarked