Model binding
Model binding takes values from the HTTP request, such as form fields, route values and the query string, and turns them into the typed parameters and objects your action expects. This lesson shows where values come from, how to control the source, and how to avoid over-posting.
- StartRequest arrivesForm fields, route values, query string, files
- Step 2Binder finds values by nameParameter id looks for a value named id
- Step 3Values are convertedText such as "5" becomes an int, dates become DateTime
- Step 4Objects are filledComplex types are built property by property
- Step 5Problems go to ModelStateConversion errors are recorded, not thrown
- EndAction runsYour parameters are ready to use
The idea in plain words
Everything in an HTTP request is text. Without model binding you would read Request.Form["Name"] and Request.Query["page"] by hand and convert every value. Model binding does that for you: it matches request values to action parameters and model properties by name, converts them to the right types and records any problems in ModelState.
For MVC controllers the binder looks in form fields, then route data, then the query string (and uploaded files). Controllers with [ApiController] read complex types from the JSON request body instead.
Choosing the source explicitly
Attributes tell the binder exactly where to look. This makes intent clear and avoids surprises when two sources have the same name.
- [FromRoute], [FromQuery], [FromForm]: route values, query string, posted form.
- [FromBody]: JSON or XML in the request body (one parameter per action).
- [FromHeader]: an HTTP header. [FromServices]: a service from dependency injection.
public class OrdersController : Controller
{
// GET /orders/list?page=2&status=shipped
public IActionResult List([FromQuery] int page = 1, [FromQuery] string? status = null)
=> View();
// POST /orders/edit/7 with form fields Quantity and Notes
[HttpPost]
public IActionResult Edit([FromRoute] int id, [FromForm] OrderEditModel model)
{
if (!ModelState.IsValid) return View(model);
// save changes for order id...
return RedirectToAction(nameof(List));
}
}
public class OrderEditModel
{
public int Quantity { get; set; }
public string? Notes { get; set; }
}Common mistakes
- Binding directly to a database entity, which lets attackers post extra fields (over-posting) such as IsAdmin. Bind to a view model with only the fields the form should change.
- Not checking ModelState.IsValid, so a failed conversion silently leaves default values like 0.
- Using [FromBody] on more than one parameter; the body can only be read once.
- Naming a parameter differently from the form field, so it never gets a value.
Interview question
What is over-posting and how do you prevent it in ASP.NET Core MVC?
Over-posting happens when a client sends extra form fields that model binding maps onto properties you did not intend to be editable, such as Price or IsAdmin on an entity. Prevent it by binding to a view model that contains only the editable fields and mapping it to the entity, or by limiting bound properties, for example with TryUpdateModelAsync and an explicit property list.
Want the full details? Read the official Microsoft documentation for this topic.