What is the difference between keep() and peek()?
MSDN says:
marks the specified key in the dictionary for retention.
returns an object that contains the element that is associated with the specified key, without marking the key for deletion.
I can't get really what the difference is, don't they both keep a value for another request?
What is the difference between keep() and peek()? MSDN says: keep(): marks the specified key in the dictionary for retention. peek(): returns an object that contains the element that is associated with the specified key, without marking the key for deletion.
In one sentence: TempData are like ViewData with one difference: They only contain data between two successive requests, after that they are destroyed. You can use TempData to pass error messages or something similar.
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.
TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered. Table of Content 1.
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, see msdn:
//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. See msdn
//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"];
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.
You have 2 good questions about how TempData works here and here
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With