In this tutorial, We are going to learn State Management in ASP.NET in ASP.NET MVC. ASP.NET MVC provides ViewData, ViewBag and TempData to persist data. Let’s start the tutorial for State Management.

After completing this tutorial you will be able to understand:

  • How to persist data in ASP.NET MVC.
  • Use of ViewData, ViewBag and TempData.
  • The difference between ViewData, ViewBag and TempData.

Introduction:

State Management is always a challenging a part for developers. ASP.NET MVC provides some important State Managment techniques. Let’s discuss them.

State Management

View Data:

ViewData is Dictionary object to pass data from Controller. It is derived from the ViewDataDictionary Class. It contains the value in form of keys and values. It is the property of ControllerBase class. In the case of any redirection, ViewData becomes null. To retrieve data from the ViewData, the Data needs to be type cast to original type. To avoid null reference exception it is required to check NULL while retrieving Data.

The syntax for storing data inside ViewData in Controller Class.


ViewData[“message”]=”Hello”;

The syntax for retrieving data from ViewData in View.



<div>@ViewData[“Message”]</div>


ViewBag:

ViewBag is also used to pass data from Controller to View. ViewBag is a wrapper around the ViewData.It is similar to ViewData with a difference is that it is a dynamic property that gets the advantage of dynamic property in C# 4.0. The life cycle of

The syntax for storing data inside ViewBag in Controller Class.

ViewBag.Message=”Hello”;

The syntax for retrieving data from ViewBag in View.




<div>@ViewBag.Message</div>



TempData:

TempData is used to keep data from one request to subsequent request this means we can persist data from Controller to View and also from Controller to Controller. Like ViewBag and ViewData, it does not destroy on redirection. TempData is derived from the TempDataDictionary Class.  It contains data in form of keys and values. It is a property of Controller Base class. The life of TempData ends when the target view is fully loaded. It is important to type cast TempData before retrieving data. Also, it is required to check for NULL values to avoid NULL reference exception.

Syntax for storing data inside TempData in Controller Class.


TempData[“Message”]=”Hello”;

The syntax for retrieving data from TempData in View.




<div>@TempData[“Message”]</div>


An example of ViewData, ViewBag and TempData (State Management) in ASP.NET MVC.

Step 1- Open Visual Studio and create an ASP.NET MVC application called StatementManagementDemo.

State Management

Step 2- Choose an Empty template and Click OK. An empty solution will be created.

Step 3- Right Click on the Controller folder and create a Controller called HomeController.

HomeController with the following code will be created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StateManagementDemo.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()
{
return View();
}

}
}

Step 4- Right Click on the Index Action method and create a View called Index. A view with the following code will be created.

@{
    ViewBag.Title = "Index";
}



<h2>Index</h2>


Step 5- Replace the Index action method with the below code.

public ActionResult Index()
{
ViewData["Message"] = "ViewData Example";
ViewBag.Message = "ViewBag Example";
TempData["Message"] = "TempData Example";
return View();

}

In the above code, we have data inside View, ViewBag and TempData respectively.

Step 6- Now we will retrieve data from ViewData and ViewBag in the View. Replace the following code from the Index.cshtml.

@{
    ViewBag.Title = "Index";
}



<h2>Index</h2>

<div>@ViewData["Message"]</div>


<div>@ViewBag.Messag</div>


Run the application and look at the output screen.

State Management

Step 7- Now we need to test TempData. Declare an Action method in HomeController.


public ActionResult Action2()
{
return View();
}

Step 8- Right Click on the Action2 method and add a View called Action2. Write the following code in the Action2.cshtml.

@{
    ViewBag.Title = "Action2";
}


<h2>Action2</h2>


<div>@TempData["Message"]</div>



Step 9- Since our default Action method is Index. To navigate to Action2 create a hyperlink in Index.cshtml file.

<a href="/Home/Action2">Get TempData</a>

Run the application and click on the link.

State Management

You will be navigated to the Action2 where you can see the data returned from the TempData.

State Management

You can download the complete source code for the application from here.

Conclusion:

Hope you understand this tutorial about State Management in ASP.NET MVC. I will appreciate your valuable feedback, comments and suggestions.

View More:

Introduction to ASP.NET MVC.

CRUD Operation in ASP.NET MVC.

Introduction to ASP.NET MVC Application Page Life Cycle.

Partial View in ASP.NET MVC.

Thank You,

Leave a Comment