編集:基本的には、複数の同じ商品を追加する現在の方法を個別にカートに追加する方法を変更したい場合は、追加されたときに入力します。編集者またはテキストボックスで数量を更新
こんにちは、私のシステムでは、カートに追加されたアイテムの数量は、買い物を続けるためにクリックすると、それを追加するたびに新しいアイテムを選択して戻るように更新されます。私はこれをエディタまたはテキストボックスのいずれかを使って行い、3回のように製品を見るために戻らずに、彼らが望む数量を選択できるようにしたいと思います。
Addメソッド(ショッピングカート・コントローラ)
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メソッド(ショッピングカートモデル)
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="[email protected]">
<td>
@Html.ActionLink(item.Product.Name, "Details", "Products", new { id = item.ProductId }, null)
</td>
<td>
@item.Product.Price
</td>
<td id="[email protected]">
@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>
}
これを解決するためのお手伝いがあれば幸いです。
どのようにしてこの商品をカートに初めて追加しましたか?これは同じことです - それは 'quantity'値と製品' id'を持つ単なる書式投稿です。 – Jasen
これを実行しようとしたときに入力が無効になったため、例を投稿してください。 –
また、最初にカートに追加する方法は、このボタン@ Html.ActionLink(「Add to cart」、「AddToCart」、「ShoppingCart」、新しい{id = Model.ID}、新しい{@class = "btn btn-info "}) –