Learn About State Management In ASP.NET MVC

In this article, we will discuss various ways to pass the data from a Controller to a View or Controller to a 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 the 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,

public ActionResult Index()
{
    ViewData["CurrTime"] = DateTime.Now;
    return View();
}

In Index.cshtml

@ViewData["CurrTime"];

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

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

ViewBag

ViewBag is also used to pass data from Controller/Action to View. It is a dynamic property that 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,

public ActionResult Index()
{
    ViewBag.CurrTime = DateTime.Now;
    return View();
}

In Index.cshtml

@ViewBag.CurrTime

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

However, if we 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 terms of performance 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 the case of Action to Action or Controller to Controller, ViewData, and ViewBag do not persist the data. It keeps the information for a single HTTP Request. It is derived from TemDataDictionary and along with this, it requires typecasting for complex data types and requires a null check to void exceptions.

Let’s understand the same in the below example.

In-Home Controller,

public ActionResult Index()
{
    TempData["CurrTime"] = DateTime.Now;
    return View();
}

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 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 the below example.

In-Home Controller,

public ActionResult Index()
{
    Session["CurrTime"] = DateTime.Now;
    return View();
}

In Index.cshtml

@Session["CurrTime"];

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

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


Similar Articles