Understanding and using ModelBinding
In this recipe, you will learn how to use ModelBinding. The ModelBinding mechanism lets the developer get/set values of UI elements to a class's properties, and vice versa.
Getting ready
We will create an ASP.NET Core MVC web application template by going to File | New | Project | AspNet Core Web Application.
How to do it...
- Let's see how nested types are handled by
ModelBinding:
public class Product
{
public int Id { get; set; }
public int Name { get; set; }
public decimal Price { get; set; }
public Category Category
{
get;
set;
}
}
public class Category
{
public int Id {
get;
set;
}
public int Name
{
get;
set;
}
}
@model R2.Models.Product
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Id"></label>
<input asp-for="Id" class="form-control" />
</div>
<div class="form-group">
<label asp-for="Name"></label>...