Model binding maps HTTP request data—route values, query strings, form fields, JSON bodies—to action parameters and complex types automatically.
Sources (priority order varies)
- Form fields → properties by name (
Email→model.Email) - Route tokens →
{id}parameters - Query string →
?page=2 - JSON body → API actions with
[FromBody]
Complex type binding
public IActionResult Create(ProductEditModel model) {
if (!ModelState.IsValid) return View(model);
// save
}
[FromQuery], [FromRoute], [FromBody]
Disambiguate when names collide or for minimal APIs—be explicit in APIs with multiple primitive parameters.
Important interview questions and answers
- Q: Model binding vs manual Request.Form?
A: Binding is declarative and testable; manual parsing is error-prone for complex types. - Q: Nullable binding failures?
A: Missing required non-nullable value types may fail validation—use nullable or defaults intentionally.
Self-check
- How does POST form data reach a view model?
- When use [FromBody]?
Pitfall: Binding POST data directly to EF entities enables over-posting—use view models with only the fields the form should set.