ViewBag in ASP.NET Core MVC

ViewBag in ASP.NET Core MVC Application

In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article before proceeding to this article where we discussed ViewData in ASP.NET Core MVC application. As we already discussed in ASP.NET Core MVC application, we can pass the data from a controller to a view using ViewData, ViewBag, and strongly typed view model. As part of this article, we are going to discuss the following pointers.

  1. What is ViewBag in ASP.NET Core MVC?
  2. How to Pass and Retrieve data From ViewBag in ASP.NET Core MVC?
  3. Example to understand ViewBag in ASP.NET Core MVC
  4. Difference between ViewData and ViewBag in ASP.NET Core MVC
What is ViewBag in ASP.NET Core MVC?

The ViewBag in ASP.NET Core MVC is one of the mechanisms to pass the data from a controller action method to a view. If you go the Controller base class, then you will find the following signature of the ViewBag property.

ViewBag in ASP.NET Core MVC

So the ViewBag is a dynamic property of the Controller base class. The dynamic type is introduced in C# 4.0. It is very much similar to the var keyword that means we can store any type of value in it but the type will be decided at run time rather than compile-time.

The ViewBag transfers the data from the controller action method to a view only, the reverse is not possible.

How to Pass and Retrieve data From ViewBag in ASP.NET Core MVC?

The point that you need to keep in mind is, ViewBag is operating on the dynamic data type. So we don’t require typecasting while accessing the data from a ViewBag. It does not matter whether the data that we are accessing is of type string or any complex type.

ViewBag in ASP.NET Core MVC with String Type:

ViewBag in ASP.NET Core MVC with String Type

ViewBag in ASP.NET Core MVC with Complex Type:

ViewBag in ASP.NET Core MVC with Complex Type

Example of ViewBag in ASP.NET Core MVC:

Let us see an example to understand how to use ViewBag to pass data from a controller to a view. We are going to work with the same example that we worked in our previous article with ViewData. So, modify the Details action method of HomeController class as shown below.

using FirstCoreMVCApplication.Models;
using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCApplication.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Details()
        {
            ViewBag.Title = "Student Details Page";
            ViewBag.Header = "Student Details";
            Student student = new Student()
            {
                StudentId = 101,
                Name = "James",
                Branch = "CSE",
                Section = "A",
                Gender = "Male"
            };
            
            ViewBag.Student = student;
            
            return View();
        }
    }
}

As you can see in the above example, here we are using the dynamic properties Title, Header, and Student on the ViewBag. 

Accessing the ViewBag in a View in ASP.NET Core MVC

Now we will see how to access the ViewBag data within an ASP.NET Core MVC view. So, modify the Details.cshtml view file as shown below. 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    <h1>@ViewBag.Header</h1>

    @{
        var student = ViewBag.Student;
    }
    <div>
        StudentId : @student.StudentId
    </div>
    <div>
        Name : @student.Name
    </div>
    <div>
        Branch : @student.Branch
    </div>
    <div>
        Section : @student.Section
    </div>
    <div>
        Gender : @student.Gender
    </div>
</body>
</html>

As you can see, here we are accessing the data from the ViewBag using the same dynamic properties Title, Header, and Student.  Now run the application and navigate to the “/Home/Details” URL and you will see the data as expected in the webpage as shown in the below image.

Output of ViewData

The ViewBag is a dynamic property which is resolved at runtime; as a result, here also it will not provide compile-time error checking as well as intelligence support. For example, if we miss-spell the property names of the ViewBag, then we wouldn’t get any compile-time error rather we came to know about the error at runtime.

Difference between ViewData and ViewBag in ASP.NET Core MVC
  1. In ASP.NET Core MVC, we can use both ViewData and ViewBag to pass the data from a Controller action method to a View.
  2. The ViewData is a weakly typed dictionary object whereas the ViewBag is a dynamic property. Both ViewData and ViewBag are used to create a loosely typed view in MVC.
  3. In ViewData, we use string keys to store and retrieve the data from the ViewData dictionary whereas in ViewBag we use the dynamic properties to store and retrieve data.
  4. Both the ViewData keys and ViewBag dynamic properties are resolved only at runtime. As a result, both do not provide compile-time error checking and because of this, we will not get intelligence support.
  5. So if we misspell the key names or dynamic property names then we will not get any compile-time error rather we came to know about the error only at run time. This is the reason why we rarely used ViewBag and ViewData in our application.

The best and preferred approach in MVC to pass data from a controller action method to a view is by using a strongly typed model object. When we use a strongly typed model object then only our view becomes a strongly typed view.

In the next article, I am going to discuss the Strongly Typed Views in ASP.NET Core MVC application with an example. Here, in this article, I try to explain ViewBag in ASP.NET Core MVC application. I hope this article will help you with your need.

Leave a Reply

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