Passing Data using ViewBag in .NET Core (Beginner Guide)
π Introduction
If you are just starting with ASP.NET Core MVC, you might wonder:
βHow do I send data from my controller to my view?β
Donβt worry π
Passing Data using ViewBag in .NET Core is one of the simplest and beginner-friendly ways to do this.
It is quick, flexible, and very easy to understand.
So, letβs learn it step by step, with clear examples, real-life scenarios, and friendly explanations β¨
π What you are going to learn in this lesson
βοΈ What ViewBag is and why it exists
βοΈ How Passing Data using ViewBag in .NET Core works
βοΈ How to send simple data (text, numbers) to a View
βοΈ How to send multiple values using ViewBag
βοΈ A real-world example you can relate to
βοΈ Common beginner mistakes (and how to avoid them)
π§ Easy real-world idea first
Think of ViewBag like a small temporary bag π
The controller puts data inside it,
and the View opens the bag and reads the data.
Simple, right? π
π§© What is ViewBag?
ViewBag is a dynamic object used in ASP.NET Core MVC.
It helps in Passing Data using ViewBag in .NET Core from a Controller to a View.
βοΈ It does not require a model
βοΈ It works only for the current request
βοΈ It is very useful for small data
π§ͺ Example 1: Passing simple text using ViewBag in .NET Core
π Controller Code
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET Core!";
return View();
}
}
π View Code (Index.cshtml)
<h2>@ViewBag.Message</h2>
β Output
Welcome to ASP.NET Core!
π§ Explanation
- The controller stores a message inside
ViewBag.Message - The view reads it using
@ViewBag.Message - So, Passing Data using ViewBag in .NET Core becomes very easy π
π§ͺ Example 2: Passing multiple values using ViewBag in .NET Core
π Controller Code
public IActionResult About()
{
ViewBag.Name = "Prashant";
ViewBag.Age = 42;
ViewBag.City = "Bhagalpur";
return View();
}
π View Code
<p>Name: @ViewBag.Name</p>
<p>Age: @ViewBag.Age</p>
<p>City: @ViewBag.City</p>
β Output
Name: Prashant
Age: 42
City: Bhagalpur
π§ Explanation
Here, multiple values are stored inside ViewBag.
Because ViewBag is dynamic, you can add any property anytime.
So, Passing Data using ViewBag in .NET Core stays flexible and fast π
π§ͺ Example 3: Passing numbers and doing calculation
π Controller Code
public IActionResult Result()
{
ViewBag.Total = 80;
ViewBag.Obtained = 65;
return View();
}
π View Code
<p>Total Marks: @ViewBag.Total</p>
<p>Obtained Marks: @ViewBag.Obtained</p>
<p>Percentage: @(ViewBag.Obtained * 100 / ViewBag.Total)%</p>
β Output
Total Marks: 80
Obtained Marks: 65
Percentage: 81%
π§ Explanation
You can even perform calculations in the View.
However, keep logic minimal in Views for clean code βοΈ
π Real-life Scenario: Showing error or success message
Imagine a form submission fails β
You want to show a quick error message without creating a full model.
π Controller
ViewBag.Error = "Something went wrong. Please try again.";
π View
@if(ViewBag.Error != null)
{
<div style="color:red">@ViewBag.Error</div>
}
π‘ Why ViewBag is useful here?
- You donβt need a model
- You can quickly pass messages
- Perfect for alerts, warnings, and notifications
Thatβs why Passing Data using ViewBag in .NET Core is very handy in real apps π
β οΈ Common beginner mistakes
β Using ViewBag for large or complex data
β Expecting ViewBag to work across multiple requests
β Misspelling property names (no compile-time check)
βοΈ Tip: For large data, always use Models or ViewModel
β¨ Quick Summary
- ViewBag is simple and dynamic
- It is best for small data
- It works between Controller and View only
- Passing Data using ViewBag in .NET Core is perfect for beginners
Youβre doing great so far π Keep going!
π Next what?
In the next chapter you will learn “Passing Data using ViewBag in .net core” π
Until then, practice these examples and try modifying the values yourself.
If you want, ask me for ViewData vs ViewBag or Model vs ViewBag next π
