Hello Friends, in this tutorial, we are going to learn the keep vs peek method in ASP.NET MVC. We are already familiar with the TempData with our previous tutorial. TempData is used to store data for the subsequent request.

After completing this tutorial you will be able to understand the following terms:

  • TempData in ASP.NET MVC.
  • keep and peek method in ASP.NET MVC.
  • The difference between keep and peek method.

keep vs peek method in ASP.NET MVC:

TempData is derived from the TempDataDictionary. We should use TempData when we need to transfer non-sensitive data from one request to another.TempData is used to store value for the subsequent request. When the subsequent request has completed the value of the TempData will be cleared.  So the point is how can we keep the value of TempData for next subsequent request. For this TempData has two methods.

  1. Keep()
  2. Peek().

Keep method has the following overloaded method:

void keep():

This method ensures that all data inside the TempData are not cleared after the completion of the request.

Void keep(string key):

This method ensures that the particular item from the Tempdata is not removed from the TempData.

Now let’s see what is peek method.

peek(string key);

if you read the TempData using peek method the TempData will persist for the next request. Here is an example of using peek method for reading data.

So we can keep our data inside the TempData safe in two ways:

  • Calling keep method after reading the value from the TempData.
  • Reading TempData value using peek method.

Let’s first discuss what happens when we don’t use keep or peek method with TempData.

See the below image.

TempData

You can see in the above picture on the first request we stored the value in the TempData and in the Second request we retrieve the value from the TempData. But we try to retrieve the value from the next request the value of the TempData will be cleared. We can keep the value of the TempData safe after calling keep method in the Second request like this

The example of the keep and peek method.

Start the Visual Studio and create an ASP.NET MVC application.

keep vs peek

Add Controller:

A blank solution will be created. Now right click on the Controller folder and create a controller called KeepController,

Write the following code in KeepController.

Add View:

Create View for respective Actions.

Action1.cshtml:

Action2.cshtml:

Action3.cshtml:

Goto Solution Explorer>App_Start folder and modify RouteConfig.cs file.

Run the application.

keep vs peek

Click on the link. If you debug the application and navigate to Action2 using the debugger.

keep vs peek

You can see in above screen TempData has the value “Hello”. This will return Hello to Action2.

keep vs peek

Now if we click on the “Go to Action3” link and check using debugger the TempData is cleared now.

keep vs peek

Here you can see the value of TempData is cleared and View will show No Value.

keep vs peek

Now Modify the Action2 Action method to keep the value of TempData Safe.

We have added TempData.keep() after we retrieve the value from the TempData. Now run the application and navigate to Action3 using the link.

keep vs peek

You can see in the above screen that the value of the TempData is not cleared after calling keep method in Action2.

An example of Peek method.

Add a Controller called PeekController. Write the following code.

If you see the code for PeekController we have used peek method to read the value of TempData instead of reading it normally. This will keep the value of the TempData safe for the particular item to the next request.

Add View:

Add View for the respective Actions and write the following code for each View.

Action1.cshtml.

Action2.cshtml.

Action3.cshml.

Now run the application and hit the following URL in the browser to check peek method.

When you navigate to Action3 using the link on the View. You can see the value of the TempData is not lost.

keep vs peek

Conclusion:

Hope you understand this tutorial about keep vs peek in ASP.NET MVC method and where to use keep and peek method in ASP.NET MVC to keep the value of TempData safe. This is also mostly asked a question nowadays. I will appreciate your feedback, comments and suggestions

View More:

Thank You.

Leave a Comment