Introduction
Passing data from an MVC controller to a view can happen in two ways, one is through the ViewModel approach using the @model statement which is indeed a strongly typed approach, and the other one is a loosely typed approach for passing small amounts of data (say title of the document or meta values) using any of the two approaches.
- ViewData
- ViewBag
What is ViewData?
ViewData is a property of the type ViewDataDictionary from the Microsoft.AspNetCore.Mvc namespace. As its type suggests, ViewData is simply put a dictionary of string key and object value types.
String type data can be passed using the ViewData object without the need for a typecast, whereas the other types when passed need to be typecasted in order to be used in the View.
The values passed using ViewData can be used in the View, and its subsequent Partials and Layout views.
ViewData property is available within the controller class, and so can be accessed within the controller method directly as ViewData[“My_Key_Name”]. Since the key is of type string, the key can also contain spaces within it, and hence ViewData[“My Key Name”] is a valid name too.
Assignment in Controller: ViewData["My_Key_Name"] = "abc";
Application in View: <span>@ViewData["My_Key_Name"]</span>
A value can also be assigned to ViewData by another approach, which is by using the ViewDataAttribute property. When a property is decorated with the ViewData attribute, it implicitly is added to the ViewData dictionary object and can also be used in the view via the ViewData approach similar to the previous approach.
Assignment in Controller:
[ViewData]
public string MyKeyName { get; set; }
Application in View:
<span>@ViewData["MyKeyName"]</span>
One difference in this approach is the key naming convention, since the key is generated from the name of the property, the general conventions of an identifier is applicable on the ViewData property name.
What is ViewBag?
ViewBag is a similar but much simpler approach than the ViewData property, which is also a property of the type DynamicViewData from the Microsoft.AspNetCore.Mvc namespace.
It can be termed as a wrapper around the ViewData object and since it is of type dynamic, no typecasting is required while accessing the values in the view.
The data to be passed on to the view using the ViewBag can be assigned in the form of dynamic properties using the dot notation and is accessed in a similar fashion on the view side.
Assignment in Controller:
ViewBag.MyKeyName = "abc"
Application in View:
<span>@ViewBag.MyKeyName</span>
One peculiar aspect of these View properties is that since both of them refer to the same ViewData object internally, the values assigned from the Controller can be used in a mix and match fashion on the View side. For example, we can add a value to the ViewData dictionary using the key MyKey as ViewData[“MyKey”] and can access the key as a property of the ViewBag object as ViewBag.MyKey.
Another aspect of these properties is that since they involve dynamic assignment and resolution, they are prone to runtime null exceptions and must be checked of nulls in order to avoid code breakage.
Performance comparison
Although both the ViewBag and ViewData objects are almost similar in performance, there’ll be a slight computational lag in the ViewBag when compared to the ViewData since the former does a bit of extra computation over the same ViewData object the latter uses and hence the ViewBag can be a little bit slower than the ViewData, but that’s very small to be acceptable.
Summary – Difference between ViewBag and ViewData in ASP.NET Core
- ViewData derives from ViewDataDictionary and holds values in the form of a Dictionary with string keys and object values. Whereas ViewBag derives from DynamicViewData and holds values in the form of dynamic property values accessible using the dot notation.
- ViewData uses string keys, and so whitespaces are allowed. It also stores values as objects and hence any other type other than strings require typecasting. On the contrary, ViewBag uses dynamic properties which resolve types at runtime and hence typecasting is not required.
- ViewBag is a wrapper around the ViewData object with a very little computational overhead, and so the values assigned can be accessed from both ViewBag and ViewData in a mix and match way.
- Both the approaches are resolved at runtime and are prone to errors, and hence the usage of these objects need to be minimal and should be used only if required.