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 understand what the difference is, don’t they keep the value for another request?

+60
asp.net-mvc tempdata
Jan 21 '14 at 8:33
source share
5 answers

When an object in TempDataDictionary is read, it will be marked for deletion at the end of this request.

This means that if you put something in TempData as

 TempData["value"] = "someValueForNextRequest"; 

And on another request you will get access to 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 a value without marking it for deletion. Say we go back to the first query where the value was stored in 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 the key that was marked for deletion that you want to keep. Retrieving an object and then saving it when deleted are two 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 save a value for another query. Use Keep when storing the value depends on additional logic.

You have 2 good questions about how TempData works here and here.

Hope this helps!

+121
Jan 21 '14 at 9:34
source share

Just finished understanding Peek and Keep and had the same confusion initially. The confusion arises from the fact that TempData behaves differently under different conditions. You can watch this video that explains Keep and Peek with a demonstration https://www.facebook.com/video.php?v=689393794478113

"Tempdata helps save values ​​for one query and MAY BE save values for the next query depending on 4 conditions."

If we understand these 4 points, you will see more clarity. Below is a chart with all 4 conditions, read the third and fourth points that refer to Peek and Keep.

enter image description here

Condition 1 (cannot be read): - If you install "TempData" inside your action, and if you do not read it in your view, then "TempData" will be saved for the next request.

Condition 2 (normal reading): - If you read "TempData" as usual, as the code below, it will not be saved for the next request.

 string str = TempData["MyData"]; 

Even if you show its normal value, for example, the code below.

 @TempData["MyData"]; 

Condition 3 (reading and saving): - If you read "TempData" and call the "Keep" method, it will be saved.

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

Condition 4 (Peek and Read): - If you read "TempData" using the "Peek" method, it will be saved for the next request.

 string str = TempData.Peek("Td").ToString(); 

Link: - http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion

+43
Sep 14 '14 at 1:00
source share

TempData strong> is also a dictionary object that remains for the duration of the HTTP request. Thus, TempData can be used to store data between controller actions on another controller action.

TempData strong> is used to check for zero values ​​each time. TempData contains two methods keep () and peek () for saving data state from one controller action to another.

When the TempDataDictionary object is read, at the end of the label request is like deleting the current read object.

The keep () and peek () methods are used to read data without deleting the current read object.

You can use Peek () when you always want to hold / prevent a value for another request. You can use Save () if this value depends on additional logic.

Overloading in TempData.Peek () and TempData.Keep () , as follows.

TempData.Keep () have 2 overloaded methods.

  • void save (): this means that all data is not deleted when the current request completes.

  • void save (string key): save a specific element in TempData using the name.

TempData.Peek () there are no overloaded methods.

  • object peek (string key): returns an object containing elements with a specific key without making a key for deletion.

An example for the return type of the TempData.Keep () and TempData.Peek () methods, as shown below.

public void Save (string key) {_retainedKeys.Add (key); }

public Peek object (string key) {object value = values; return value }

+5
Dec 02 '14 at 5:46
source share

Don't they store the value for another query?

Yes, they do, but when the first one is void , the second returns and object :

 public void Keep(string key) { _retainedKeys.Add(key); // just adds the key to the collection for retention } public object Peek(string key) { object value; _data.TryGetValue(key, out value); return value; // returns an object without marking it for deletion } 
+1
Jan 21 '14 at 9:32
source share

The Keep () method marks the specified key in the dictionary for storage

You can use Keep () when the value of the prevent / hold parameter depends on additional logic.

when you read TempData data and want to save for another request, then use the keep method so that TempData can be accessed for the next request, as in the example above.

-one
Sep 08 '19 at 12:34 on
source share



All Articles