TempData is used to pass data from current request to subsequent request (means redirecting from one page to another). It’s life is very short and lies only till the target view is fully loaded. But you can persist data in TempData by calling Keep() method.
TempData with Keep method
If you want to keep value in TempData object after request completion, you need to call Keep method with in the current action. There are two overloaded Keep methods to retains value after current request completion.
void Keep()
Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.
@{ var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting TempData.Keep(); // retains all strings values }
void Keep(string key)
Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.
@{ var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting TempData.Keep("emp"); // retains only "emp" string values }
keep() vs peek()
When an object in a TempDataDictionary
is read, it will be marked for deletion at the end of that request.
That means if you put something on TempData like
TempData["value"] = "someValueForNextRequest";
And on another request you access it, the value will be there but as soon as you read it, the value will be marked for deletion:
//second request, read value and is marked for deletion
object value = TempData["value"];
//third request, value is not there as it was deleted at the end of the second request
TempData["value"] == null
The Peek
and Keep
methods allow you to read the value without marking it for deletion. Say we get back to the first request where the value was saved to TempData.
With Peek
you get the value without marking it for deletion with a single call:
//second request, PEEK value so it is not deleted at the end of the request
object value = TempData.Peek("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
With Keep
you specify a key that was marked for deletion that you want to keep. Retrieving the object and later on saving it from deletion are 2 different calls.
//second request, get value marking it from deletion
object value = TempData["value"];
//later on decide to keep it
TempData.Keep("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
Tempdata helps to preserve values for a single request and CAN ALSO preserve values for the next request depending on 4 conditions”.
You can use Peek
when you always want to retain the value for another request. Use Keep
when retaining the value depends on additional logic.
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.
string str = 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.
string str = TempData.Peek("Td").ToString();
Great post! and the graph made it even clearer! Thanks so much.
LikeLike
Amazing post! It certainly cleared my concept to a great extent.
LikeLike
great your article is very understandable and very simple,easy explanation with flow diagram.
LikeLike
Excellent elaboration for “TempData” concept.
LikeLike
Excellent explaination
LikeLike
The graph is making is much clear,
Thanks
LikeLike
Very useful explanation about how to use “TempData”.
Thanks!!
LikeLike
great
LikeLike