Lets pretend we have a /Cart/Checkout view that lists every Product in the Cart, plus a TextBox with the Quantity (in my real world application I have more than Quantity). How would you update the Quantity? I have used the following code, but I'am looking for a "cleaner" way of doing it.
View
<% int i = 0; foreach(var product in Model.Products) { %>
<p>
<%= Html.Encode(product.Name) %>
<%= Html.TextBox("Quantity[" + i + "]", product.Quantity) %>
</p>
<% i++; } >
Controller
[AcceptVerbs(HttpVerbs.Post)]
// This would be a better way, but it does not work, any ideas?
//public ActionResult Checkout([Bind(Include = "ProductID, Quantity")] Product[] products)
public ActionResult Checkout(int[] productID, int[] Quantity)
{
// Get the Product from cartRepository, update Quantity and SaveChanges().
}
How would you solve it?