【问题标题】:CheckOut Cart MVC结帐购物车 MVC
【发布时间】:2017-06-17 20:43:17
【问题描述】:

我正在尝试在我的网上商店项目中为购物车创建一个结帐。 我希望每次您的新闻结帐时都会从模型 Clothes.Amount 1 为您在购物车会话中拥有的每种产品。 然后我想清理购物车。

在购物车控制器中,我有这三个功能和一个要订购的功能,但我认为这对这个问题并不重要。 错误就在这行

Item.Cl.Amount--;
Delete(Item.Cl.Id);

这里是代码

private int isExisting(int id)
        {
            List<Item> cart = (List<Item>)Session["cart"];
            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].Cl.Id == id)
                    return i;
            }return -1;
        }
public ActionResult Delete(int idDelete)
        {
            int index = isExisting(idDelete);
            List<Item> cart = (List<Item>)Session["cart"];
            cart.RemoveAt(index);
            Session["cart"] = cart;

            return View("Order"); 

        }
public ActionResult CheckOut(int idCheck)
        {
            int index = isExisting(idCheck);
            if(index != -1) { 
                foreach (Item item in (List<Item>)Session["cart"]) {
                    Item.Cl.Amount--;
                    Delete(Item.Cl.Id);
                }
            }
            return View();
        }

【问题讨论】:

    标签: c# asp.net-mvc checkout webshop


    【解决方案1】:

    根据您的问题,我无法看到您遇到的错误。但是单看你的代码我会认为这是经典的“集合被修改;枚举操作可能无法执行”。

    在方法CheckOut 中,您正在迭代会话中的项目列表。当你这样做时,你调用Delete 方法从列表中删除一个项目。这根本是不允许的……

    如果这是您的问题,您可以将第二种方法更改为以下内容:

    public ActionResult CheckOut(int idCheck)
            {
                int index = isExisting(idCheck);
                if(index != -1) { 
                    foreach (Item item in ((List<Item>)Session["cart"]).ToList()) {
                        Item.Cl.Amount--;
                        Delete(Item.Cl.Id);
                    }
                }
                return View();
            }
    

    【讨论】:

      猜你喜欢
      • 2019-11-02
      • 2014-11-09
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2017-12-01
      • 2014-03-06
      • 1970-01-01
      相关资源
      最近更新 更多