Click here to Skip to main content
15,891,726 members
Articles / Programming Languages / C# 5.0
Tip/Trick

How to Persist Data with TempData in MVC

Rate me:
Please Sign up or sign in to vote.
4.93/5 (6 votes)
13 Nov 2014CPOL2 min read 48.1K   4   1
This tip presents an example of TempData to persist data in between the request.

Introduction

This tip presents an example of TempData to persist data in between the request.

TempData is used to pass data from current request to subsequent request (i.e., redirecting from one page to another). Its life is too short and lies only till the target view is fully loaded. But you can persist data in TempData by calling the method Keep ().

In real-time scenarios, we need to keep the value in TempDate object after request completion also.

In that scenario, we have two overloaded methods to retain value after current request completion.

  1. Void Keep()
  2. Void Keep(string Key)

Now, we will see them one at a time with proper examples.

Using the Code

1. Void Keep()

In Visual Studio intelligence will tell like ‘Marks all keys in the dictionary for retention’ i.e., it will hold all the items in TempData. When calling this method within the current action ensures that all the items in TempData are holding not removing at the end of the current request.

Example

We will do these in two ways either view level or action method in controller level:

View-Level
C++
//
// View level source code
//

@model SampleTempDataProject.Models.UserModel;

@{
  Layout="~/Views/Shared/_Layout.cshtml";
  ViewBag.Title="TempData Example";
  Var tempDataUserPersist= (User) TempData["User"];
  TempData.Keep();
}

//
Action -Controller Level

First, we need to include the model:

C++
//
// action get method code
//

using SampleTempDataProject.Models.UserModel;

public ActionResult Index()
{
   Var UserList= db.User.ToList();
   TempData["User"]= UserList;
   TempData.Keep();
   return View();
}

2 . Void Keep(string Key)

In Visual Studio intelligence will tell like ‘Marks the specified key in the dictionary for retention’ i.e., it will hold specified item in TempData. When calling this method within the current action ensures that specified item in TempData is holding not removing at the end of the current request.

Example

We will do these in two ways either view level or action method in controller level.

View-Level
C++
//
// View level source code
//

@model SampleTempDataProject.Models.UserModel;

@{
  Layout="~/Views/Shared/_Layout.cshtml";
  ViewBag.Title="TempData Example";
  Var tempDataUserPersist= (User) TempData["User"];
  TempData.Keep("User");
}

//
Action -Controller Level

First, we need to include the model:

C++
//
// action get method code
//

using SampleTempDataProject.Models.UserModel;

public ActionResult Index()
{
   Var UserList= db.User.ToList();
   TempData["User"]= UserList;
   TempData.Keep("User");
   return View();
}

If you want to access the TempData:

C++
//
// action Post method code
//
[HttpPost]
public ActionResult Index()
{
   Var UserPersist=  TempData["User"];
   TempData.Keep("User");
   return View();
}

Points of Interest

I really like this very much. Initially, I faced this kind of storing data problem in tempdata. After I got this, it was perfect for me. Hope you enjoyed this article, and hope it is helpful for you. Thanks for your support. Cheers...

I sincerely thank my supporters.

History

  • 2014-11-13

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Question5 Pin
#realJSOP10-Jan-18 6:14
mve#realJSOP10-Jan-18 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.