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 :

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.

here 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

ViewBag,ViewData can’t pass data back to controller.

Keep Cool Coding…