0

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. :)

Stian
  • 1,522
  • 2
  • 22
  • 52

1 Answers1

0

You don't have to make it type="submit". you can make it normal Ajax request.

(1) you can add id in attr (ex: data-id = "@Id") and make javascript ajax call when press delete call this function and read attr value.

(2) you can pass an array in JSON format read more from here https://www.codeproject.com/Questions/834557/Pass-an-array-to-MVC-Controller-from-ajax How to pass Array to MVC Controller with Jquery?

Ahmed
  • 1,542
  • 2
  • 13
  • 21
  • Thank you, but I'm not using JQuery in this project, and I have no experience with it. Is there no other way? – Stian Jan 24 '18 at 19:29
  • you don't have to make Ajax call using JQuery https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery – Ahmed Jan 25 '18 at 07:34