Question

I have a cart solution I'm building for a client, and I'm stuck with the basket. The basket is created using 2 tables, CartID and CartItems.

On the Basket page, I just read in the items for the cart and display them:

var basketowner = Request.Cookies["cart"].Value;
var basket = db.Query("SELECT * FROM CartItems WHERE CartID=@0", basketowner);

These are then listed as so:

<form action="" method="post">
@foreach(var row in basket){

        <div class="basketcolumn1">@row.ProductID<input type="hidden" value="@row.CartItemID" name="LineID" /></div>
        <div class="basketcolumn2">@row.ProductTitle @row.ProductOrientation</div>
        <div class="basketcolumn3">@row.ProductSize</div>
        <div class="basketcolumn1"><input type="text" id="quantity" name="quantity" maxlength="2"value="@row.quantity" style="width: 20px;" /></div>
        <div class="basketcolumn4">&nbsp;</div>
        <div class="basketcolumn5">@currencysymbol.Currency@row.Price</div>
        <div class="basketcolumn6"><a href="~/Basket-Delete/@row.CartItemID" class="basketdelete">x</a></div>
        <div class="basketcolumn6"><a href="~/Basket-Update/@row.CartItemID">Update</a></div>

        <div class="clear"></div>
    }

<input type="submit" value="Update" id="submit" name="submit"/>
</form>

What need to do is submit each of the quantities back to the CartItems table and update. Is there a way to do this with a single form?

If not, can I submit the form variable (quantity) to a seperate page to update one at a time?

Any help much appreciated.

Was it helpful?

Solution

You should try a solution like this one:

assign ProductId as name to each input field

<input type="text" id="quantity" name="@row.ProductID" maxlength="2" 
    value="@row.quantity" style="width: 20px;" />

in if (IsPost) scan input fields and update your table

if(IsPost){
  foreach (string key in Request.Form){
    db.Execute(@"UPDATE CartItems SET quantity = @0 WHERE CartID = @1 AND
        ProductID = @2", Request.Form[key], basketowner, key);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top