Please enable Javascript to correctly display the contents on Dot Net Tricks!

Persisting Data with TempData

  Author : Shailendra Chauhan
Posted On : 25 Feb 2014
Total Views : 156,976   
Updated On : 25 Feb 2014
 

TempData is used to pass data from current request to subsequent request (means redirecting from one page to another). It’s life is very short and lies only till the target view is fully loaded. But you can persist data in TempData by calling Keep() method.

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.
  1. void Keep()

    Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.

     @model MyProject.Models.EmpModel;
    @{ 
     Layout = "~/Views/Shared/_Layout.cshtml"; 
     ViewBag.Title = "About";
     var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting 
     TempData.Keep(); // retains all strings values 
    } 
  2. void Keep(string key)

    Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.

     @model MyProject.Models.EmpModel;
    @{ 
     Layout = "~/Views/Shared/_Layout.cshtml"; 
     ViewBag.Title = "About";
     var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting 
     TempData.Keep("emp"); // retains only "emp" string values 
    } 

Key point about TempData and TempData.Keep()

  1. Items in TempData will only tagged for deletion after they have read.

  2. Items in TempData can be untagged by calling TempData.Keep(key).

  3. RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.

Summary

In this article you have learned how to persist data in TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS (0)
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+