;

WHEN TO USE KEEP() VS PEEK() WITH TEMPDATA IN ASP.NET MVC


Tutorialsrack 19/02/2021 C# ASP.NET MVC

In this article, you will learn when to use the Keep() method and when to use the Peek() method with TempData in ASP.NET MVC.  As you know, TempData is used to pass the data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. 

The lifespan of TempData is very short and it stores the data temporarily and When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request. 

This means if you put some value in TempData like below:

TempData["value"] = "someValueForNextRequest";

And on another request you can access it, the value will be there but as soon as you read the value of TempData, 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

But you can persist the data of the TempData by using Keep() and Peek() Method.

The Keep() and Peek() methods allow you to read the value without marking it for deletion. So we get back to the first request where the value was saved to TempData.

Keep() Method

With the Keep() method, 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"];

Peek() Method

With the Peek() method, 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"];

Now Question is When To Use Peek() Method and When To Use Keep() Method

You can use the Peek() method when you always want to retain the value for another request in a single call. Use the Keep() method when retaining the value depends on additional logic.

Basically, Tempdata helps to preserve values for a single request and Can also preserve values for the next request depending on 4 conditions as given below:

  1. Not Read
  2. Normal Read
  3. Read and Keep
  4. Peek and Read

Condition 1 (Not Read): If you set the value of TempData inside your action method, and you do not read it in your view, then the TempData will be persisted for the next request.

Condition 2 (Normal Read): If you read the TempData normally like in the given below example, then it will not be persisted for the next request.

String str = TempData["value"];

Or even if you displaying it on View, it is a normal read like the code below:

<h1>@TempData["value"]</h1>

Condition 3 (Read and Keep): if you read the TempData and then call the Keep() method, the TempData value will be persisted for the next Request.

@TempData["value"];

TempData.Keep("value");

Condition 4 (Peek and Read): if you read the TempData by using the Peek() method, the value of the TempData will be persisted for the next request.

String str = TempData.Peek("value").ToString();

I hope this article will help you to understand when to use the Keep() method and when to use the Peek() method with TempData in ASP.NET MVC.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags