Complete C# Tutorial

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 ✨

🧠 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

  1. The controller stores a message inside ViewBag.Message
  2. The view reads it using @ViewBag.Message
  3. 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 😊

Leave a Comment

fourteen + sixteen =

Share this Doc

Passing Data Using ViewBag

Or copy link