ASP.NET Core Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

ASP.NET Core MVC – ViewBag

Back to: ASP.NET Core Tutorial

2. ViewBag

Type: dynamic (wrapper around ViewData)

Scope: From Controller to View

✅ Use Case:

Similar to ViewData, but with cleaner syntax using dynamic properties.

✅ Example:

Controller:


csharp
public IActionResult Index() { ViewBag.Message = "Hello from ViewBag"; return View(); }

View (Razor):


html
<h2>@ViewBag.Message</h2>

⚠️ Considerations:

  • Also loosely typed

  • No compile-time checking

Scroll to Top