MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

Want to Use TempData, Peek And Keep In ASP.NET MVC

Published on 15 February 16
0
0
we will learn the uses of Peek and Keep with TempData in ASP.NET MVC application.

We will continue from the previous article; let us Set TempData["TempModel"] = "This is TempData Example"; in HomeController.


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 1
Set Break Point for TempData and now just run the application and check the value.


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 2
For first call after one request we didn't fetch value in our view so TempData["TempModel"] keeps the value; see the following screen's watch window, it keeps the value which we set in Controller.


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 3
Now, read the TempData Value from View, and try to check the Value,


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 4
Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 5
It will become null because you have read the data.


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 6
Now, create one more TempData["TempModelKeep"], here we will first read the data, and for the next request we want to keep the data, so set the Value using TempData. Keep the method as in the following screenshot:


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 7
TempData["TempModel"] became null but TempData["TempModelKeep"] keeps the data for the next request.


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 8
Now, let's use Peek method in our View. For that declare one string variable and set the TempData Value in it by writing the following code:


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 9
Run your application and check the value for TempData["TempModel"].


Want to Use TempData, Peek And Keep In ASP.NET MVC - Image 10
If you read TempData value using Peek then the data will be available for the next request. Use Peek and keep smartly in your application code.
  • If you set value for TempData and do not read the value then the data will be available for next request.
  • If you set value for TempData and read in View then the data will be deleted or will be null.
  • If you read TempData in the first request and want to keep the value for the next request then use 'Keep' Method.
  • If you read the TempData using 'Peek' then value persists for the next request also.

This blog is listed under Open Source and Development & Implementations Community

Related Posts:

ASP DOT NET MVC (ASP.NET MVC)

 

ASP DOT NET (ASP.NET)

 
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
 
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top