简体   繁体   English

如何通过一个更新按钮更新购物车中所有商品的数量

[英]How to update the quantity of all items in the cart with one update button

I have the following action method, when I press the update button on my cart and post to this method I need it bind all productId and partquantity values into the respective parameters/arrays (int[] ProductId, int[] partquantity) and it does this.我有以下操作方法,当我按下购物车上的更新按钮并发布到此方法时,我需要将所有 productId 和 partquantity 值绑定到相应的参数/数组(int [] ProductId,int [] partquantity)中这个。 I am presuming when form data, that is keys and values are posted they arrive in some sort of order, likely as elements are laid out on the HTML page (top to bottom)?我假设当表单数据,即键和值被发布时,它们以某种顺序到达,可能是因为元素被布置在 HTML 页面上(从上到下)? So I wish for the operation on each cart item to be performed using the correct partquantity entered, that is for the correct productId.因此,我希望使用输入的正确零件数量对每个购物车项目执行操作,即正确的 productId。 I am guessing if they post and bind in strict order then partquantity[2] should be the correct quantity for ProductId[2] etc. ?我猜如果他们以严格的顺序发布和绑定,那么 partquantity[2] 应该是 ProductId[2] 等的正确数量?

The below logic in trying to increment f by 1 for each operation on each productId in the ProductId[] array does not work.对于 ProductId[] 数组中的每个 productId 的每个操作,尝试将 f 递增 1 的以下逻辑不起作用。 I need to get this to work because say I have 5 items added to the cart and change the quantity for 4 of them I wish to just press the one update button and it will update for all these items\lines in the cart.我需要让它工作,因为假设我有 5 件商品添加到购物车并更改其中 4 件的数量我希望只需按下一个更新按钮,它将更新购物车中的所有这些商品\行。 So method needs to catch all the posted productId and quantities and use in the correct order, so the right quantity is assigned to the right cart item which is looked up by ProductId.因此方法需要捕获所有已发布的 productId 和数量并以正确的顺序使用,因此将正确的数量分配给 ProductId 查找的正确购物车项目。

public RedirectToRouteResult UpdateCart(Cart cart, int[] ProductId, int[] partquantity, string returnUrl)
{
   int f = 0;
   int x = partquantity.Length;

   while (f <= x) 
   {
     foreach (var pid in ProductId)
     {
       f++;
       var cartItem = cart.Lines.FirstOrDefault(c => c.Product.ProductID == pid);
       cartItem.Quantity = partquantity[f];
     }
   }
   return RedirectToAction("Index", new { returnUrl });
 }

This is the View:这是视图:

<% foreach (var line in Model.Cart.Lines)
 { %>
  <tr>
    <td align="center"><%: Html.TextBox("partquantity", line.Quantity)%></td>
    <td align="left"><%: line.Product.Name%></td>
    <td align="right"><%: line.Product.ListPrice.ToString("c")%></td>
    <td align="right">
    <%: (line.Quantity * line.Product.ListPrice).ToString("c")%>
    </td>
  </tr>
<%: Html.Hidden("ProductId", line.Product.ProductID)%>
<% } %>

Custom Binder自定义活页夹

    public class CartModelBinder : IModelBinder
{

    private const string cartSessionKey = "_cart";

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {

        if (bindingContext.Model != null)
            throw new InvalidOperationException("Cannot update instances");

        Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];

        if (cart == null)
        {
            cart = new Cart();
            controllerContext.HttpContext.Session[cartSessionKey] = cart;

        }

        return cart;

    }
}

} }

Wouldn't it be easier to have an Object - say BasketItem that has the productId and the quantity as Properties?拥有一个 Object 会不会更容易 - 比如具有 productId 和数量作为属性的 BasketItem? so you would have one array/list/ienumerable to pass on to update and to bind.所以你会有一个数组/列表/ienumerable 传递给更新和绑定。

Your problem would be obsolete since the connection between quantity and productid is done via the object.您的问题将过时,因为数量和 productid 之间的连接是通过 object 完成的。

Your Cart object should be enough as parameter for this scenario, you can have something like this.您的购物车 object 应该足以作为这种情况的参数,您可以拥有这样的东西。

The general idea would be to use an index so you get your Cart object with your Lines as you passed them to the view originally but with updated Quantity values.一般的想法是使用一个索引,这样当您最初将它们传递给视图但具有更新的数量值时,您将获得带有线条的购物车 object。

Your model as I understand it:据我了解,您的 model :

public class Cart
{
  ...
  public List<CartItem> Lines {get; set; }
}

public class CartItem
{
  public Product Product {get; set;}
  public int Quantity {get; set;}
  ...
}

In your view:在您看来:

@model Cart
...
@using(Html.BeginForm())
{
  @{ int index = 0; }
  @foreach(var l in Model.Lines)
  {
    @Html.Hidden("cart.Lines.Index", index);
    @Html.Hidden("cart.Lines[" + index + "].Product.ProductID", l.Product.ProductID)
    @Html.TextBox("cart.Lines[" + index + "].Quantity")
    @{ index++; }
  }
  <input type="submit" value="Update Quantity" />
}

Your controller:您的 controller:

public ActionResult UpdateCart(Cart cart)
{
  // you should have new values on Quantity properties of the cart.Lines items.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM