Introduction
In ASP.NET MVC, communication between the Controller and View is very important.
The Controller processes requests and sends data to the View.
The View collects user input and sends it back to the Controller.
This two-way communication helps build dynamic web applications such as Create, Read, Update, Delete (CRUD) systems.
In this article, we will learn:
How to pass data Controller → View
How to pass data View → Controller
Different techniques
A simple CRUD example
Everything is explained in simple beginner-friendly steps.
MVC Architecture Overview
MVC stands for:
| Component | Responsibility |
|---|---|
| Model | Handles data and business logic |
| View | Displays UI |
| Controller | Handles user requests |
Example flow:
User → Controller → Model → Controller → View → User
Ways to Pass Data from Controller to View
There are four common methods.
| Method | Type |
|---|---|
| ViewBag | Dynamic object |
| ViewData | Dictionary object |
| TempData | Temporary storage |
| Model | Strongly typed object |
1 Passing Data Using ViewBag
ViewBag is a dynamic property used to pass small data.
Controller
public ActionResult Index()
{
ViewBag.Name = "Abhay";
ViewBag.City = "Ahmedabad";
return View();
}
View
<h2>Name: @ViewBag.Name</h2>
<h2>City: @ViewBag.City</h2>
What is this?
Output
Name: AbhayCity: AhmedabadWhen to Use
Small data
Simple messages
2 Passing Data Using ViewData
ViewData works like a dictionary.
Controller
public ActionResult Index()
{
ViewData["Course"] = "ASP.NET MVC";
return View();
}
View
<h2>@ViewData["Course"]</h2>
What is this?
3 Passing Data Using TempData
TempData stores data until the next request.
Controller
public ActionResult Index()
{
TempData["Message"] = "Record Saved Successfully";
return RedirectToAction("Display");
}
public ActionResult Display()
{
return View();
}
View
<h2>@TempData["Message"]</h2>
What is this?
4 Passing Data Using Model (Best Method)
This is the most recommended approach.
Step 1 Create Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Step 2 Controller
public ActionResult Details()
{
Student s = new Student()
{
Id = 1,
Name = "Abhay",
Age = 22
};
return View(s);
}
Step 3 View
@model Student
<h2>ID: @Model.Id</h2>
<h2>Name: @Model.Name</h2>
<h2>Age: @Model.Age</h2>
Passing Data From View to Controller
Now let’s see how user input goes from View → Controller.
This is done using HTML forms.
Simple CRUD Example
We will build a simple Student CRUD system.
Step 1 Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Step 2 Controller
public class StudentController : Controller
{
static List<Student> students = new List<Student>();
public ActionResult Index()
{
return View(students);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Student s)
{
students.Add(s);
return RedirectToAction("Index");
}
}
Step 3 Create View
@model Student
@using (Html.BeginForm())
{
<label>Name</label>
@Html.TextBoxFor(m => m.Name)
<br/>
<label>Age</label>
@Html.TextBoxFor(m => m.Age)
<br/>
<input type="submit" value="Save"/>
}
Here the form sends data to the controller.
Step 4 Display Data
View
@model List<Student>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.Name</td>
<td>@item.Age</td>
</tr>
}
</table>
Flow of Data in CRUD
User enters data in View ↓ Form submits to Controller ↓ Controller receives Model ↓ Data saved ↓ Controller sends updated data to View ↓ View displays result
Important Concepts
Model Binding
Model binding automatically converts form values into model objects.
Example:
Name → Student.Name
Age → Student.Age
MVC automatically maps them.
Best Practice for Beginners
Always prefer Model-based data passing instead of ViewBag or ViewData.
Example:
Good
return View(student);
Not recommended
ViewBag.Name = "Rahul";
Quick Comparison
| Method | Strongly Typed | Usage |
|---|---|---|
| ViewBag | No | Small data |
| ViewData | No | Simple data |
| TempData | No | Between requests |
| Model | Yes | Best for real applications |
Conclusion
Passing data between Controller and View is one of the most important concepts in ASP.NET MVC development.
Developers can use:
ViewBag
ViewData
TempData
Models
However, for real-world applications and CRUD operations, the Model approach is the most powerful and recommended method.
Join the conversation! Your thoughts help the community grow.