How to use ViewBag in ASP .Net MVC C#?

ViewBag uses the dynamic feature that was introduced in C# 4.0. It allows an object to have properties dynamically added to it. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.

ViewBag only transfers data from controller to view, not vice-versa. ViewBag values will be null if redirection occurs. ViewBag is able to set and get value dynamically and able to add any number of additional fields without converting it to strongly typed.

Syntax

Following is the syntax for storing data in ViewBag −

ViewBag.PropertyName = value;

Following is the syntax for retrieving data from ViewBag −

var data = ViewBag.PropertyName;

How ViewBag Works

ViewBag is a dynamic wrapper around ViewData that provides a more convenient syntax for passing data from controller to view. It uses the DynamicViewDataDictionary class internally and allows you to access data using dot notation rather than dictionary-style string keys.

ViewBag Data Flow Controller ViewBag.Data = value; View @ViewBag.Data One-way transfer Dynamic properties created at runtime No compile-time type checking

Using ViewBag with Simple Data Types

Example

using System;
using System.Web.Mvc;

namespace DemoMvcApplication.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            ViewBag.CurrentDate = DateTime.Now;
            ViewBag.Count = 42;
            ViewBag.IsActive = true;
            
            return View();
        }
    }
}

Corresponding view to display the ViewBag data −

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>Current Date: @ViewBag.CurrentDate</p>
<p>Count: @ViewBag.Count</p>
<p>Status: @(ViewBag.IsActive ? "Active" : "Inactive")</p>

Using ViewBag with Collections

Example

using System.Collections.Generic;
using System.Web.Mvc;

namespace DemoMvcApplication.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {
            ViewBag.Countries = new List<string> {
                "India",
                "Malaysia", 
                "Dubai",
                "USA",
                "UK"
            };
            
            ViewBag.CountryCount = ViewBag.Countries.Count;
            return View();
        }
    }
}

The corresponding view to iterate through the collection −

@{
    ViewBag.Title = "Countries List";
}
<h2>Countries List (@ViewBag.CountryCount countries)</h2>
<ul>
@foreach(string country in ViewBag.Countries) {
    <li>@country</li>
}
</ul>

ViewBag vs ViewData vs TempData

Feature ViewBag ViewData TempData
Type Dynamic Dictionary Dictionary
Syntax ViewBag.Property ViewData["key"] TempData["key"]
Compile-time checking No No No
Survives redirect No No Yes

Common Use Cases

  • Passing simple data like page titles, messages, or status information

  • Transferring collections for dropdowns, lists, or navigation menus

  • Sending configuration data or display preferences to views

  • Providing metadata about the current page or user session

Output

When you run the countries list example, the output will be −

Countries List (5 countries)
? India
? Malaysia  
? Dubai
? USA
? UK

Conclusion

ViewBag provides a convenient dynamic way to pass data from controllers to views in ASP.NET MVC. While it offers flexibility and ease of use, remember that it lacks compile-time type checking and only works for one-way data transfer from controller to view.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements