Introduction
When a user submits a form in ASP.NET MVC, the data entered in the form needs to go to the Controller.
But how does this happen automatically?
This process is called Model Binding.
Model Binding is one of the most powerful features in ASP.NET MVC because it automatically maps form data to C# objects.
In this article, we will learn:
What Model Binding is
How it works
Step-by-step example
Why it is useful
What is Model Binding?
Model Binding is a process that converts user input (form data) into C# objects automatically.
Example:
User enters:
Name = Abhay
Age = 22
Model Binding converts it into:
Student obj = new Student();
obj.Name = "Abhay";
obj.Age = 22;
Why Do We Use Model Binding?
Without Model Binding, we would have to write code like this:
string name = Request["Name"];
int age = Convert.ToInt32(Request["Age"]);
This is difficult and messy.
Model Binding makes code:
✔ Cleaner
✔ Faster
✔ Easier to understand
Step-by-Step Example
We will create a simple Student Form.
Step 1: Create Model
Models → Student.cs
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
Step 2: Create Controller
Controllers → HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Student student)
{
ViewBag.Name = student.Name;
ViewBag.Age = student.Age;
return View();
}
}
Important Point
public ActionResult Index(Student student)
Here Model Binding automatically fills the student object.
Step 3: Create View
Views → Home → Index.cshtml
@model Student
<h2>Student Form</h2>
@using (Html.BeginForm())
{
<label>Name:</label>
@Html.TextBoxFor(m => m.Name)
<br /><br />
<label>Age:</label>
@Html.TextBoxFor(m => m.Age)
<br /><br />
<input type="submit" value="Submit" />
}
@if (ViewBag.Name != null)
{
<h3>Output</h3>
<p>Name: @ViewBag.Name</p>
<p>Age: @ViewBag.Age</p>
}
What is this?
Output
User enters:
Name: Abhay
Age: 22
Output:
Name: Abhay
Age: 22
How Model Binding Works Internally
Flow:
Form Input (HTML)
↓
Request Data
↓
Model Binder
↓
C# Model Object
↓
Controller
Model Binder matches:
Input field name
Model property name
Important Rule
Input names must match model properties.
Example:
<input name="Name" />
What is this?
Matches:
public string Name { get; set; }
Real-World Example
Imagine a registration form.
User enters:
Model Binding converts all data into a User object automatically.
Advantages of Model Binding
Less Code: No need to manually read form values.
Easy to Use: Works automatically.
Clean Structure: Improves readability.
Common Mistake
If names do not match, binding will fail.
Example wrong:
<input name="username" />
What is this?
But model:
public string Name { get; set; }
Fix: Names must match.
Conclusion
Model Binding is a very important feature in ASP.NET MVC. It automatically converts user input into C# objects, making development faster and easier.
In this article, we learned:
What Model Binding is
How it works
Step-by-step example
Why it is useful