Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TempData keep() vs peek()

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.

I can't get really what the difference is, don't they both keep a value for another request?

like image 600
Ghasan غسان Avatar asked Jan 21 '14 08:01

Ghasan غسان


People also ask

What is the difference between TempData keep () and Peek () function?

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.

What is difference between TempData and ViewData?

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.

Is there any way to keep or preserve TempData value to multiple request?

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.

When should we use TempData in MVC?

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.


1 Answers

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!

like image 86
Daniel J.G. Avatar answered Oct 03 '22 23:10

Daniel J.G.