Simple Differences b/w ViewBag and ViewData in ASP.NET Core

In this brief article, let us discuss the differences between ViewBag and ViewData and how they are used in ASP.NET Core MVC

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.

  1. ViewData
  2. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *