Nested forms in ASP.NET MVC

asp.net-mvc

Solution

We've done this on our project by using an individual form for Remove, and a completely different form for Update. They both go to different POST actions in the CartController.

UPDATE: Example given (in raw HTML):

<form action="/cart/updatequantity" method="post"> 
    <input type="hidden" name="ProductSku" value="ABC-123" /> 
    <input name="quantity" class="quantity" size="2" maxlength="2" type="text" value="1" /> 
    <input type="submit" value="Update" /> 
</form> 

<form action="/cart/removeitem" method="post"> 
    <input type="hidden" name="productSku" value="ABC-123" /> 
    <input type="submit" value="Remove" /> 
</form> 

Problem

I have a shopping cart where the following are true: - There is one "Remove" button for each product in the shopping cart - There is one editable quantity text box for each product in the shopping cart - There is one "Update" button for the entire shopping cart The idea is that the user can modify the quantities for each product in the cart and click "Update" to commit the changes. How would you program the "Update" button using MVC? Would you wrap the entire shopping cart in a form that posts back to itself and somehow locate the quantity values in the FormCollection? The problem with that approach is that since the "Remove" buttons each live in their own forms I would now be doing nested forms on the page and I am not even sure that is allowed. ``` <% using (Html.BeginForm("Index", "Cart")) { %> <table> <tr> <th>&nbsp;</th> </tr> <% foreach (var item in Model) { %> <tr> <td> <input name="qty" type="text" value="<%=item.Quantity%>" maxlength="2" /> <% using (Html.BeginForm("RemoveOrderItem", "Cart")) { %> <%= Html.Hidden("ShoppingCartItemID", item.ShoppingCartItemID) %> <input name="add" type="submit" value="Remove" /> <%} %> </td> </tr> <% } %> </table> <input name="update" type="submit" value="Update" /> <%} %> ``` How would I incorporate the bottom input into this form?

Original source

Related problems