Exemple #1
0
        /// <summary>
        /// 未登录情况下的购物车
        /// </summary>
        /// Dic
        private void OfflineCart()
        {
            // 1.取现有的购物车信息
            IDictionary <int, int> existDict = CookieHelper.Get <IDictionary <int, int> >("cart_dict") ?? new Dictionary <int, int>();

            #region 添加

            //int productId = Request["item"].ToInt32();
            // 判断是否是添加购物车操作
            int productId;
            if (Int32.TryParse(Request["item"], out productId))
            {
                // 判断ID 的合法性
                Books b = bookbll.QuerySingle(productId);
                if (b != null)
                {
                    // 判断当前购物车中是否已经存在当前这个商品
                    if (existDict.ContainsKey(productId))
                    {
                        // 存在
                        existDict[productId]++;
                    }
                    else
                    {
                        // 不存在
                        existDict.Add(productId, 1);
                    }
                }
            }
            #endregion

            if (existDict.Count > 0)
            {
                CartList = cartbll.GetOfflineCartInfos(existDict.Keys.ToArray()).ToList();

                foreach (var item in CartList)
                {
                    item.Count = existDict[item.ProductId];
                }
            }
            else
            {
                CartList = new List <CartInfo>();
            }
            // 设置回客户端
            CookieHelper.Set("cart_dict", existDict, DateTime.Now.AddYears(1));
        }