I have an action that returns a model to the View which is IEnumerable<T>
. In the view I loop through the list using foreach. T type has a property called Amount.
Now when I click SAVE
button, I want to POST the model (IEnumerable) to an action. The IEnumerbale items, their properties Amount
should contain the correct values.
When I submit it, in the action, the model is null.
For testing the IEnumerable<T>
is IEnumerable<Produt>
public class Product
{
public string Title { get; set; }
public int Amount { get; set; }
}
view display products:
@model IEnumerable<Product>
<form asp-controller="Home" asp-action="Order" method="post" role="form">
@foreach (var product in Model)
{
<div>
<span>@product.Title</span>
<input asp-for="@product.Amount" type="text">
</div>
}
<button type="submit">SAVE</button>
</form>
controller post action:
[HttpPost]
public async Task<IActionResult> Order(IEnumerable<Product> model)
{
}