Dot Net Stuff

How to Persist Data with TempData


TempData in Asp.Net MVC is one of the very useful feature. It is used to pass data from current request to subsequent request. In other words if we want to send data from one page to another page while redirection occurs, we can use TempData, but we need to do some consideration in code to achieve this feature in MVC. Because the life of TempData is very short and lies only till the target view is fully loaded. But, we can use Keep() method to persist data in TempData.

Using Keep method of TempData

As we know from above discussion to keep value in TempData object after request completion, we need to use Keep method. Keep methods should be call within current action to persist data in TempData.

NOTE: There are two overloaded method of Keep method. We should use correct method as par our requirement. The first method doesn’t take any argument and it will persist all the string values stored in TempData. While the other overloaded method will ask for a string key argument, which you can use to restrict any specific value with that key.

Points to remember about TempData and TempData.Keep()

  • TempData is a dictionary object derived from the TempDataDictionary class.
  • TempData is used to pass data from the current request to a subsequent request, in other words in the case of redirection.
  • The life of a TempData is very short and it retains its value for a short period of time.
  • It requires typecasting for complex data type.
  • The Items stored in TempData will only tagged for deletion after they have read.
  • This Items stored in TempData can be untagged for deletion by calling TempData.Keep(key).
  • RedirectResult and RedirectToRouteResult always calls TempData.Keep() to retain items in TempData.

Here is the sort example of using both overloaded methods to persist data in TempData.

void Keep()

By Calling Keep() method with in the current action ensures that all the items in TempData are not removed at the end of the current request. Following example show how you can use Keep method to persist the data in TempData.

 
@model MyProject.Models.StudentModel;
	@{ 
	 Layout = "~/Views/Shared/_Layout.cshtml"; 
	 ViewBag.Title = "Student";
	 var tmpStudent = TempData["student"] as StudentModel; //need typcasting 
	 TempData.Keep(); // retains all strings values 
	} 

void Keep(string key)

By Calling Keep(string key) method with in the current action ensures that specific item in TempData is not removed at the end of the current request. Following example show how you can use Keep method to persist the data in TempData.

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

Summary:In this article we have learned that how can we 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.


Keen to hear from you...!

If you have any questions related to what's mentioned in the article or need help with any issue, ask it, I would love to here from you. Please MakeUseOf Contact and i will be more than happy to help.

About the author

Anil Sharma is Chief Editor of dotnet-stuff.com. He's a software professional and loves to work with Microsoft .Net. He's usually writes articles about .Net related technologies and here to shares his experiences, personal notes, Tutorials, Examples, Problems & Solutions, Code Snippets, Reference Manual and Resources with C#, Asp.Net, Linq , Ajax, MVC, Entity Framework, WCF, SQL Server, jQuery, Visual Studio and much more...!!!

Loading