Tag Archives: ViewData

Difference between ViewBag, ViewData, TempData in ASP.NET MVC

ASP.NET MVC provide ViewData, ViewBag and TempData for maintaining the data between controller to view, controller to action, action to action.

Similarities between ViewBag & ViewData :

  • ViewBag & ViewData are used to maintain data while travelling from controller to view.
  • This communication is only for server call, it becomes null if redirect occurs.
  • It’s life span is short because the data will became null when redirection occurs. The main pupose of viewData and ViewBag is to maintain the data while travelling from controller to view.

ViewData

  • ViewData is a dictionary object. It is derived from viewDataDictionary class.
  • Typecasting is required for complex data type and need to checks for null values to avoid error

ViewBag

  • ViewBag is a dynamic property (The properties that are associated with the dynamic are ignored at the compile time).
  • It doesn’t require typecasting.

TempData

  • TempData is derived from TempDataDictionary class
  • Tempdata helps to store/preserve values within in a single request. TempData preserve values for the next request based on 4 different conditions in MVC. More about temp data click here (It helps to maintain the data when we move from one controller to another controller or from one action to another action).
  • It requires typecasting for complex data type and checks for null values to avoid error.

 

Keep Cool Coding….

 

State Management in ASP.NET MVC – ViewData, ViewBag and TempData in MVC

In ASP .NET MVC, ViewData, View Bag, TempData is used to maintain the state in our page/view. Viewdata, ViewBag is used to transfer date/information from controller to view in the current request. TempData use to transfer data from controller to another controller. It has capable of maintaining the data to not only on current request, it can maintain the data in next HTTP requests.

Flow Diagram :

statemanagement in MVC -dotnet-helpers

ViewData

ViewData is a dictionary object, which used to maintain data from controller to view. It is derived from viewDataDictionary class. It requires typecasting for complex data and need to check null values to avoid error as shown below.

Note: The data inside the viewdata will be available in the current request only so the value become NULL if redirection occurs.

Example:

Controller

public ActionResult Index()
{
var dotnetproduct = new dotnetproduct(name: “MVC”);
ViewData[“dotnet-product”] = dotnetproduct;

ViewData[“Name”] = “dotnet-helpers-ViewBag”;
return View();
}

View : 

@ViewData[“Name”]

@{
var ViewDataproduct = ViewData[“dotnetproduct”] as dotnet-product; <!– Need to Typecast –>
if (@ViewDataproduct != null) <!– Need to check for NULL value –>
{
// Your Logic
}
}

ViewBag

ViewBag is a dynamic property (The  properties that are associated with the dynamic are ignored at the compile time). And it has same purpose of viewdata for maintain the data from controller to view. It doesn’t require typecasting.

Note: The data inside the viewdata will be available in the current request only so the value become NULL if redirection occurs.

Example:

Controller

public ActionResult Index()
{
ViewBag.Name = “dotnet-helpers-ViewData”;
return View();
}

View :

@ViewData.Name

TempData

TempData is a dictionary derived from TempDataDictionary class. It differ from ViewBag and ViewData by it’s  life cycle of the object. TempData keeps the data up to next HTTP Request.

Note: The data inside the Tempdata will be available in the next request. In simple, it helps to maintain data when we move from one controller to other controller or from one action to other action. It requires typecasting for complex data type and check for null values to avoid error.

Controller

public ActionResult Index()
{
TempData[“Name”] = “dotnet-helpers-TempData”;
return RedirectToAction(“CheckTempData“);
}

public ActionResult CheckTempData()
{
var model= TempData[“Name”];
return View(Name);
}

View :

@ViewData[“Name”]

Output : dotnet-helpers-TempData

Where to Use?

  • ViewBag is a dynamic object, so we can add strongly typed objects, primitive values, etc that we need. We can choose ViewBag while passing dynamic data from view to controller.
  • ViewData is a ViewDataDictionary, it can be accessed through a key (string). We can choose ViewDate while passing data from view to controller.
  • ViewData need typecasting while getting the original object/data.
  • TempData is a dictionary derived from TempDataDictionary class. It can be used when we need to carry data from one controller to another controller or one action to another action. It also requires typecasting while getting the original object/data. The data had removed when view reads them. For  more detail about TempData click  here
  • ViewBag,ViewData can’t pass data back to controller.

Keep Cool Coding…