EDIT: Basically want to change the current method of adding more than one of the same product from adding them individually to the cart to when one has been added you can just input how many you want.
Hi guys so currently in my system the quantity of an item added to the cart is updated by the user clicking to continue shopping and going back and selecting a new item each time they wish to add it. I would like for this to be done through either an editor or text box so they can select the quantity they want without having to go back to view the product like 3 times.
Add method (Shopping Cart Controller)
public ActionResult AddToCart(int id)
{
var addedProduct = db.Products.Single(product => product.ID == id);
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedProduct);
return RedirectToAction("Index");
}
Add method (Shopping Cart Model)
public void AddToCart(Product product)
{
var cartItem = db.Carts.SingleOrDefault(c => c.CartId == ShoppingCartId && c.ProductId == product.ID);
if (cartItem == null)
{
cartItem = new Cart
{
ProductId = product.ID,
CartId = ShoppingCartId,
Count = 1,
DateCreated = DateTime.Now
};
db.Carts.Add(cartItem);
}
else
{
cartItem.Count++;
}
db.SaveChanges();
}
shopping cart view model
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
}
Shopping Cart View @{ ViewBag.Title = "Store Checkout"; CultureInfo us = new CultureInfo("en-GB"); }
<h3 class="text-center">
<span><img src="~/Content/Images/shoping_cart.png" />Your shopping cart:</span>
</h3>
<div id="update-message" class="text-info">
</div>
@if (Model.CartItems.Count == 0)
{
<a class="btn-danger" href="~/Products/Index">Your shopping cart is empty, continue shopping---></a>
}
else
{
<table class="table-responsive table-bordered table-striped">
<tr>
<th>
Product Name
</th>
<th>
Price (each)
</th>
<th>
Quantity
</th>
<th>Sub-total</th>
<th></th>
</tr>
@foreach (var item in Model.CartItems)
{
<tr id="row-@item.ProductId">
<td>
@Html.ActionLink(item.Product.Name, "Details", "Products", new { id = item.ProductId }, null)
</td>
<td>
@item.Product.Price
</td>
<td id="item-count-@item.ProductId">
@item.Count
</td>
<td>
@((item.Product.Price * item.Count).ToString("c", us))
</td>
<td>
<a href="" class="RemoveLink" data-id="@item.ProductId">
Remove from cart
</a>
</td>
</tr>
}
<tr>
<td>
Total
</td>
<td></td>
<td></td>
<td id="cart-total" class="text-success">
<b>@Model.CartTotal.ToString("C", us)</b>
</td>
</tr>
</table>
<p class="button">
<a>@Html.ActionLink("Continue Shopping", "Index", "Products")</a>
</p>
<p class="button">
@Html.ActionLink("Click and Collect Order>> ", "AddressAndPayment", "Checkout") @Html.ActionLink("Checkout With Braintree>> ", "AddressAndPaymentBraintree", "Checkout")
</p>
}
Any Help to solving this would be much appreciated.