Recently I was taking MVC class in Mumbai and I saw there was lot of confusion among participantson how MVC tempdata , Peek and Keep works. I think the confusion stems because most of the MVC developers know only thehalf truth.
So the half thing which most of MVC developer 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 send’s a request to a 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.