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.
State Management is always a challenging a part for developers. ASP.NET MVC provides some important State Managment techniques. Let’s discuss them.
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.
The syntax for retrieving data from ViewData in View.
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.
The syntax for retrieving data from ViewBag in View.
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.
The syntax for retrieving data from TempData in View.
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.
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.
Step 4- Right Click on the Index Action method and create a View called Index. A view with the following code will be created.
Step 5- Replace the Index action method with the below code.
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.
Run the application and look at the output screen.
Step 7- Now we need to test TempData. Declare an Action method in HomeController.
Step 8- Right Click on the Action2 method and add a View called Action2. Write the following code in the Action2.cshtml.
Step 9- Since our default Action method is Index. To navigate to Action2 create a hyperlink in Index.cshtml file.
Run the application and click on the link.
You will be navigated to the Action2 where you can see the data returned from the TempData.
You can download the complete source code for the application from here.
Hope you understand this tutorial about State Management in ASP.NET MVC. I will appreciate your valuable feedback, comments and suggestions.