Tag Archives: How to Persist Data with TempData using Peek and Keep in MVC

State Management in ASP.NET MVC – ViewData, ViewBag and TempData in MVC

In ASP .NET MVC, ViewData, View Bag, TempData is used to maintain the state in our page/view. Viewdata, ViewBag is used to transfer date/information from controller to view in the current request. TempData use to transfer data from controller to another controller. It has capable of maintaining the data to not only on current request, it can maintain the data in next HTTP requests.

Flow Diagram :

statemanagement in MVC -dotnet-helpers

ViewData

ViewData is a dictionary object, which used to maintain data from controller to view. It is derived from viewDataDictionary class. It requires typecasting for complex data and need to check null values to avoid error as shown below.

Note: The data inside the viewdata will be available in the current request only so the value become NULL if redirection occurs.

Example:

Controller

public ActionResult Index()
{
var dotnetproduct = new dotnetproduct(name: “MVC”);
ViewData[“dotnet-product”] = dotnetproduct;

ViewData[“Name”] = “dotnet-helpers-ViewBag”;
return View();
}

View : 

@ViewData[“Name”]

@{
var ViewDataproduct = ViewData[“dotnetproduct”] as dotnet-product; <!– Need to Typecast –>
if (@ViewDataproduct != null) <!– Need to check for NULL value –>
{
// Your Logic
}
}

ViewBag

ViewBag is a dynamic property (The  properties that are associated with the dynamic are ignored at the compile time). And it has same purpose of viewdata for maintain the data from controller to view. It doesn’t require typecasting.

Note: The data inside the viewdata will be available in the current request only so the value become NULL if redirection occurs.

Example:

Controller

public ActionResult Index()
{
ViewBag.Name = “dotnet-helpers-ViewData”;
return View();
}

View :

@ViewData.Name

TempData

TempData is a dictionary derived from TempDataDictionary class. It differ from ViewBag and ViewData by it’s  life cycle of the object. TempData keeps the data up to next HTTP Request.

Note: The data inside the Tempdata will be available in the next request. In simple, it helps to maintain data when we move from one controller to other controller or from one action to other action. It requires typecasting for complex data type and check for null values to avoid error.

Controller

public ActionResult Index()
{
TempData[“Name”] = “dotnet-helpers-TempData”;
return RedirectToAction(“CheckTempData“);
}

public ActionResult CheckTempData()
{
var model= TempData[“Name”];
return View(Name);
}

View :

@ViewData[“Name”]

Output : dotnet-helpers-TempData

Where to Use?

  • ViewBag is a dynamic object, so we can add strongly typed objects, primitive values, etc that we need. We can choose ViewBag while passing dynamic data from view to controller.
  • ViewData is a ViewDataDictionary, it can be accessed through a key (string). We can choose ViewDate while passing data from view to controller.
  • ViewData need typecasting while getting the original object/data.
  • TempData is a dictionary derived from TempDataDictionary class. It can be used when we need to carry data from one controller to another controller or one action to another action. It also requires typecasting while getting the original object/data. The data had removed when view reads them. For  more detail about TempData click  here
  • ViewBag,ViewData can’t pass data back to controller.

Keep Cool Coding…

 

How to Persist Data with TempData using Peek and Keep in MVC

In this post we are going to discuss about how to preserve the value in next request using TempData Peek and Keep in ASP.NET MVC.

What is TempData?

Tempdata helps to store/preserve values within in a single request. This is the one of the concept for maintaining state in ASP .Net MVC.

How to store/persist data in next request?

TempData preserve values for the next request in 4 different conditions in MVC. They are

  • Condition 1 – Not Read in First Request.
  • Condition 2 – Read In First Request.
  • Condition 3 – Read & persist using Keep.
  • Condition 4 – Persist using Peek and Read.

Let us discuss this one by one in detail.

Not Read in First Request : If we do not read “TempData” in the current request then “TempData” value will be persisted for the next request.
Read In First Request : If we read “TempData” in the current request then “TempData” value will be not persist for the next request.
Read & Persist using Keep : If we read “TempData” in the current request and we can keep method to persist TempData for the next request. In MVC, we are having void keep() and  void keep(string key) methods to persist the data.

Example :

var readInfo = TempData

[“dotnet-helpers-info”];
TempData.Keep(“dotnet-helpers-info”);
OR
var readInfo = TempData[“dotnet-helpers-info”];
TempData.Keep();

Persist using Peek and Read : If we read “TempData” using the Peek method then then the data will persist in the next request.

Example :

var readInfo = TempData.Peek(“dotnet-helpers-info”);

Flow Chart