Communication between the Controller and View is crucial in ASP.NET MVC.

  • Data is sent to the View by the Controller once it has processed requests.
  • User input is gathered by the View and returned to the Controller.

CRUD (Create, Read, Update, Delete) systems and other dynamic web applications are made possible by this two-way communication.

This essay will teach us:

  • How to pass Controller → View data
  • How to send data from View to Controller
  • Various methods
  • A basic example of CRUD

Everything is explained in simple beginner-friendly steps.

MVC Architecture Overview

MVC stands for:

ComponentResponsibility
Model Handles data and business logic
View Displays UI
Controller Handles user requests

Flow example:
Ways to Transfer Data from Controller to View: User → Controller → Model → Controller → View → User

There are four common methods.

MethodType
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 = "Scott";
    ViewBag.City = "London";

    return View();
}

View
<h2>Name: @ViewBag.Name</h2>
<h2>City: @ViewBag.City</h2>


What is this?
Output
Name: Scott 
City: London

When 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 = "Scott",
        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 = "Peter";

 

Quick Comparison

MethodStrongly TypedUsage
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.