From Controller to View: Passing Data in .NET Core
Passing Data to Views (ViewData, ViewBag, TempData, Strongly Typed Models)
When you start learning MVC in .NET Core, one thing becomes very important.
How does data move from the controller to the view?
At first, this feels confusing.
However, once you understand it, everything starts making sense.
In fact, this topic is a turning point for many beginners.
Think of it this way.
The controller prepares the data.
The view shows the data.
So, something must connect them.
That “something” is what this chapter is all about.
Why Passing Data to Views Is Important
Every real website shows data.
For example, user names, product lists, prices, or messages.
Without data, a view is just empty HTML.
Because of this, learning how to pass data is essential.
Moreover, it helps you write clean and professional code.
As a result, your confidence grows step by step.
Don’t worry if you feel slow right now.
Everyone starts here.
And yes, you will master this with practice.
Ways to Pass Data to Views in .NET Core
.NET Core gives us four main ways to pass data to views.
Each one has its own use case.
So, let’s understand them one by one with simple examples.
Don’t try to memorize everything now.
Instead, just focus on the idea.
We will learn each topic in detail in upcoming lessons.
1️⃣ ViewData – Simple Key and Value
ViewData works like a dictionary.
You store data using a key.
Then you read it in the view.
Controller
public IActionResult Index()
{
ViewData["Message"] = "Hello from ViewData";
return View();
}
View
<p>@ViewData["Message"]</p>
This is easy to understand.
However, you must remember the key name.
Also, type safety is not very strong here.
Still, it is good for small data.
2️⃣ ViewBag – Cleaner and Friendly
ViewBag feels more natural.
It looks like a normal object.
So beginners usually like it more.
Controller
public IActionResult Index()
{
ViewBag.Message = "Hello from ViewBag";
return View();
}
View
<p>@ViewBag.Message</p>
This looks clean and readable.
Because of that, many developers use it for simple values.
But internally, it still works like ViewData.
3️⃣ TempData – Short-Lived Data
Sometimes, you want data only for the next request.
For example, after saving a form, you want to show a success message.
That’s where TempData helps.
Controller
TempData["Success"] = "Data saved successfully!";
return RedirectToAction("Index");
View
<p>@TempData["Success"]</p>
TempData is temporary.
Once it is read, it disappears.
Therefore, it is perfect for redirects and alerts.
4️⃣ Strongly Typed Models – The Best Practice
Now comes the most powerful method.
Strongly Typed Models are used in real-world projects.
Here, you create a class.
Then you send that object to the view.
Model
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
Controller
public IActionResult Index()
{
Student s = new Student
{
Name = "Rahul",
Age = 20
};
return View(s);
}
View
@model Student
<p>Name: @Model.Name</p>
<p>Age: @Model.Age</p>
This method is clean and safe.
Moreover, it is easy to maintain.
That’s why professionals prefer it.
Which One Should You Use?
In the beginning, everything may look similar.
But slowly, clarity comes.
Use:
-
ViewData or ViewBag for small values
-
TempData for redirect messages
-
Strongly Typed Models for real applications
Over time, choosing the right option becomes natural.
What’s Coming Next?
Right now, you’ve seen only a demo-level overview.
And that’s perfectly okay.
In the next upcoming lessons, we will:
-
Go deep into each concept
-
Write more examples
-
Explain common mistakes
-
Build real scenarios
So stay relaxed.
You’re exactly where you should be.
Learning takes time.
But trust me, you are moving forward.
