ASP.NET Core MVC Interview Question #35

How does MVC bind complex objects?

Controllers, Actions, Model Binding & Action Results Junior Beginner

Quick Interview Answer

For a complex object, model binding creates the object and attempts to populate its bindable properties from matching request keys. Nested property names and indexed collection names can also be bound when the submitted field names follow the expected format.

Detailed Explanation

For a complex object, model binding creates the object and attempts to populate its bindable properties from matching request keys. Nested property names and indexed collection names can also be bound when the submitted field names follow the expected format.

A dedicated input or ViewModel type is preferable to binding persistence entities directly because it clearly defines which values a user is allowed to submit.

Code Example

public class CustomerInputModel
{
    public string Name { get; set; } = string.Empty;
    public AddressInputModel Address { get; set; } = new();
}

[HttpPost]
public IActionResult Save(CustomerInputModel model)
{
    return View(model);
}