2016-05-12 1 views
0

編集:基本的には、複数の同じ商品を追加する現在の方法を個別にカートに追加する方法を変更したい場合は、追加されたときに入力します。編集者またはテキストボックスで数量を更新

こんにちは、私のシステムでは、カートに追加されたアイテムの数量は、買い物を続けるためにクリックすると、それを追加するたびに新しいアイテムを選択して戻るように更新されます。私はこれをエディタまたはテキストボックスのいずれかを使って行い、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> 
} 

これを解決するためのお手伝いがあれば幸いです。

+0

どのようにしてこの商品をカートに初めて追加しましたか?これは同じことです - それは 'quantity'値と製品' id'を持つ単なる書式投稿です。 – Jasen

+0

これを実行しようとしたときに入力が無効になったため、例を投稿してください。 –

+0

また、最初にカートに追加する方法は、このボタン@ Html.ActionLink(「Add to cart」、「AddToCart」、「ShoppingCart」、新しい{id = Model.ID}、新しい{@class = "btn btn-info "}) –

答えて

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> 
} 

更新アクションJasenが、私は私の目的を果たすsimplierフィックスを見つけた答えのための

[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); 
} 
0

こんにちはおかげです。基本的に私のショッピングカートのテーブルに新しい行を作成し、これにそれを追加しました@Html.ActionLink("Add Another?", "AddToCart", "ShoppingCart", new { id = item.ProductId }, new { @class = "btn btn-info" })他人を助けるかもしれませんが、あなたの答えはありがとうが、これは私のために働いた。

関連する問題