I have a shopping cart form with any number of items (products). For each item, there is a delete-button. At the bottom of the form, there is an update-quantities-button. Each of the delete-buttons should post "delete" and Id. The update-button should post "update" and the entire array of values from the input fields.
I have spent some time googling for answers, but every mention of "multiple submit buttons in one form" I could find, refers to different actions per button. I want to point to the same action, but with a different Id for each button. That means I cannot use asp-action="someAction" asp-route-id="someId"
in the FORM
-tag.
My questions:
1) How to send action and Id to the controller (for delete item)? If I put the Id inside value=""
in the button-tag, the Id will be displayed on the button. I don't want that.
2) How to send the array of values from all the item's quantity input-field, and how to match each value to the appropriate Id (in the action method)?
This is my cart:
<form>
<table>
@foreach (var item in Model.ShoppingCartItems)
{
<tr>
<td nowrap>
<a asp-action="Details" asp-route-id="@item.ProductId">
@item.ProductTitle
</a>
</td>
<td nowrap>
@Html.DisplayFor(Model => item.ProductPrice)
</td>
<td nowrap>
<input asp-for="@item.Quantity" value="@item.Quantity" />
</td>
<td nowrap>
@Html.DisplayFor(modelItem => item.LineSum)
</td>
<td>
<button type="submit" formaction="Delete">Delete</button>
</td>
</tr>
}
<tr>
<td colspan="3"><strong>Sum</strong></td>
<td nowrap><strong>@Html.DisplayFor(model => model.TotalSum)</strong></td>
<td></td>
</tr>
<tr>
<td colspan="3">
<button type="submit" formaction="Update">Update quantities</button>
</td>
<td colspan="2">
<a asp-action="Checkout">Go to checkout</a>
</td>
</tr>
</table>
</form>
The action methods haven't been written yet. I'm not expecting anyone to code for me, I just need a nudge in the right direction. :)