コード例 #1
0
        public ActionResult AddToCart(int productID)
        {
            Dictionary <int, Item> dct    = new Dictionary <int, Item>();
            List <Item>            lst    = new List <Item>();
            List <Item>            lstNew = new List <Item>();

            if (Session["cart"] != null)
            {
                lst = (List <Item>)Session["cart"];
                var temp     = hivm.lstCart(productID);
                var unionlst = lst.Union(temp);
                foreach (var item in unionlst)
                {
                    if (!dct.ContainsKey(item.product.ProductID))
                    {
                        dct.Add(item.product.ProductID, item);
                    }
                    else
                    {
                        item.quantity += lst.Where(v => v.product.ProductID == item.product.ProductID).Select(av => av.quantity).FirstOrDefault();
                        dct[item.product.ProductID] = item;
                    }
                }
                lstNew          = dct.Select(v => v.Value).ToList();
                Session["cart"] = lstNew;
            }
            else
            {
                Session["cart"] = hivm.lstCart(productID);
            }
            return(RedirectToAction("Index"));
        }