I spend a fair amount of time trying to understand how to bind an ASP.net Core 5 MVC form to an Entity property consisting of a List of another Entities. After all my goofs, it was surprisingly simple. Here is the solution I found:
Below is the model. First create the MainEntity with a property that is a List of SubEntities.
// The Model
public Class MainEntity
{
public List<SubEntity> Examples {get; set:}
}
public Class SubEntity
{
public string Example {get; set;}
public string ExampleTwo {get; set }
}
In the controller, bind the form to the Entity like the following:
public async Task<IActionResult> Edit(Guid id, [Bind("SubEntity")] MainEntity mainEntity)
{
// ... your code here
return RedirectToAction(nameof(Index));
}
The form (in my case generated by Javascript) should look something like:
<input asp-for="Examples[i].Example" />
<input asp-for="Examples[i].ExampleTwo" />
The "i" above should be incremented in a for loop for indexing.