State Management in Asp.net MVC

State management in Asp.net MVC

There are several ways to maintaine state in MVC are as below.

  • ViewBag : Transfer data from controller to view, No typecasting required because it is a dynamic property.
  • ViewData : Transfer data from controller to view but typecasting required on the view.this is dictionary of objects that is derived from ViewDataDictionary.
  • TempData  : Helps to maintain data when you move from one controller to another controller or from one action to another action. In other words when you redirect, tempdata helps to maintain data between those redirects. It internally uses session variables.

View Management

Implementing ViewData, ViewBag and TempData with Example:

Viewbag code inside controller

 

 

ViewData:

 

TempData: