Learn About State Management In ASP.NET MVC

In this article, we will discuss various ways to pass the data from a Controller to View or Controller to Controller. In ASP.NET web applications, for state management, we use ViewState, HiddenFileds, Session, etc.

Now, ASP.NET MVC doesn’t support ViewState or Server controllers.

To achieve state management in ASP.NET MVC, there are different ways which are given below.

  • ViewData
  • ViewBag
  • TempData
  • Sessions

Let’s have a detailed discussion on each of them.

ViewData

If you want to pass the data from Controller/Action to View, then you must use ViewData. It is derived from ViewDataDictionary class. Along with this, it requires typecasting for complex datatypes and it requires a null check to void exceptions.

Let’s understand the same with an example.

In Home Controller,

  1. public ActionResult Index()  
  2.         {  
  3.             ViewData["CurrTime"] = DateTime.Now;  
  4.             return View();  
  5.         }  

In Index.cshtml,

@ViewData["CurrTime"];

Now, in the above example, we have stored the data in CurrTime ViewData and reading its value in View, i.e., Index.cshtml.

Note

If you want to view the value, then keep breakpoint in Index action method and press F11.

ViewBag

ViewBag is also used to pass data from Controller/Action to View. It is a dynamic property which uses the Dynamic feature of C# 4.0. And, the Dynamic keyword internally uses reflection.

Let’s see in the below example.

In Home Controller,

  1. public ActionResult Index()  
  2.         {  
  3.             ViewBag.CurrTime = DateTime.Now;  
  4.             return View();  
  5.         }  

In Index.cshtml,

@ViewBag.CurrTime

Now, in the above example, we have stored the data in CurrTime ViewBag and reading its value in View, i.e., Index.cshtml.

However, if we will look at the syntax of ViewBag, it is a bit complicated. To overcome this issue, we can use ViewData. ViewBag is nothing but it is a syntactical layer over ViewData i.e. it simplifies syntax,  which means ViewData internally uses ViewBag.

Note

Syntactically, ViewData is complex but it is faster than ViewBag in term of performanc since ViewBag internally uses reflection.

TempData

TempData is used to pass the data from Action to Action or Controller to Controller, and then to View. In case of Action to Action or Controller to Controller, ViewData and ViewBag do not persist the data. It keeps the information for single HTTP Request. It is derived from TemDataDictionary and along with this, it requires typecasting for complex data types and it requires a null check to void exceptions.

Let’s understand the same in the below example.

In Home Controller,

  1. public ActionResult Index()  
  2.         {  
  3.             TempData["CurrTime"] = DateTime.Now;  
  4.             return View();  
  5.         }  

In Index.cshtml,

@TempData["CurrTime"];

Now, in the above example, we have stored data in CurrTime Tempdata and are reading its value in View, i.e., Index.cshtml.

Since TempData is only for a single request and if you perform some another request for the same action, then its value will not persist. That one request can go to Action to Action or Action to View.

To overcome this issue, we can use Sessions.

Session

If you want to store the data and maintain it for multiple requests, then use session variables.

Let’s understand the same in below example.

In Home Controller,

  1. public ActionResult Index()  
  2.         {  
  3.   
  4. Session["CurrTime"] = DateTime.Now;  
  5.             return View();  
  6.         }  

In Index.cshtml,

@Session["CurrTime"];

Now, in above example, we have stored the data in CurrTime Session variable and are reading its value in View, i.e., Index.cshtml.

If you go to the browser and press F5 and check the value before execution of Session["CurrTime"] = DateTime, you will see the previous value.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now