编辑:基本上想要更改添加多个相同产品的当前方法,将它们单独添加到购物车中,当添加一个时,您可以输入您想要的数量。
大家好,目前在我的系统中,用户点击继续购物并返回并在每次添加新项目时更新添加到购物车的商品数量。我希望通过编辑器或文本框完成此操作,以便他们可以选择他们想要的数量而无需返回查看产品3次。
添加方法(购物车控制器)
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");
}
添加方法(购物车模型)
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();
}
购物车视图模型
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
}
购物车视图 @ { ViewBag.Title =“商店结账”; 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>
}
非常感谢任何解决此问题的帮助。
答案 0 :(得分:0)
这是基本表单提交
@using(Html.BeginForm("UpdateQuantity", "ShoppingCart", FormMethod.Post))
{
<input name="cartId" value="@cart.Id" type="hidden" />
<input name="productId" value="@product.Id" type="hidden" />
<input name="quantity" type="text" />
<button type="submit">Update</button>
}
更新操作
[HttpPost]
public ActionResult UpdateQuantity(int cartId, int productId, int quantity)
{
var cart = db.Carts.FirstOrDefault(c => c.CartId == cartId);
cart.Count = quantity;
db.SaveChanges();
return RedirectToAction("MyCart", routeValues: new { cartId = cartId });
}
[HttpGet]
public ActionResult MyCart(int cartId)
{
var cart = db.Carts.FirstOrDefault(c = c.CartId == cartId);
return View(cart);
}
答案 1 :(得分:0)
嗨,谢谢Jasen的答案,但我找到了一个更简单的修复程序,符合我的目的。基本上在我的购物车表中创建了一个新行,并将其添加到@Html.ActionLink("Add Another?", "AddToCart", "ShoppingCart", new { id = item.ProductId }, new { @class = "btn btn-info" })
感谢您的回答,虽然它可能对其他人有所帮助,但这对我有用。