首页 > 学院 > 开发设计 > 正文

Mvc4.0添加商品到Cookie

2019-11-14 16:45:47
字体:
来源:转载
供稿:网友
/// <summary>/// 添加Cookie/// </summary>/// <param name="gc">GoodsToCookie</param>public static int AddCookies(GoodsCookie gs){string userID = null;if (HttpContext.Current.session["hasLogin"] != null){userID = HttpContext.Current.Session["hasLogin"].ToString();//登录}ShoppingMallContext db = new ShoppingMallContext();var cookies = HttpContext.Current.Request.Cookies["MyShoppingCart"]; //创建Cookie并命名if (cookies == null){string strCookie = gs.skuID + "-" + gs.count + ",";cookies = new HttpCookie("MyShoppingCart") //对象初始化{Expires = DateTime.Today.AddDays(1),//过期时间Value = strCookie//Cookie赋值};HttpContext.Current.Response.Cookies.Add(cookies);//添加进Cookieif (userID != null)//如果用户登录了{SkuShoppingCart sc = new SkuShoppingCart();//初始化购物车类sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID.Where(f => f.UserID == userID).Select(f => f.ShoppingCartID).Single();sc.SkuCount = gs.Count;//统计数量db.SkuShoppingCarts.Add(sc);//添加进数据库}return 1;}else{if (String.IsNullOrWhiteSpace(cookies.Value))//判断Cookies里值为不为空{string strCookie = gs.skuID + "-" + gs.count + ",";cookies = new HttpCookie("MyShoppingCart") //对象初始化{Expires = DateTime.Today.AddDays(1),Value = strCookie};HttpContext.Current.Response.Cookies.Add(cookies);//添加进Cookieif(userID != null){SkuShoppingCart sc = new SkuShoppingCart();sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID.Where(f => f.UserID == userID).Select(f => f.ShoppingCartID).Single();sc.SkuCount = gs.Count;//统计数量db.SkuShoppingCarts.Add(sc);//添加进数据库}return 1;}else//返之{#region -----添加到字典-----Dictionary<int, int> dict = new Dictionary<int, int>();//初始化字典string goods = cookies.Value;//声明一个string变量并赋值 为 Cookiegoods = goods.Substring(0, goods.Length - 1);//指定字符串长度string[] skuidArray = goods.Split(',');//拆解字符串并赋值给skuidArray数组int count = 0;//声明一个Int变量foreach (var i in skuidArray)//迭代数组{if(i =="" || i == null)//如果没有值{continue;//继续}else//如果有{string[] strCont = i.Split('-');//拆解字符串int key = int.Parse(strCont[0]);//第一个int value = int.Parse(strCont[1]);//第二个//判断字典里有没有这个keyif (dict.ContainsKey(key)){dict[key] += value;//如果有就叠加}else//没有{dict.Add(key, value);//添加进字典}}}#endregion#region ------- 字典判断 -------if (dict.ContainsKey(gs.skuID))//如果字典里有这个ID{dict[gs.skuID] += gs.count;count += skuidArray.Count();//就让他们的数量相加}else{dict.Add(gs.skuID, gs.count);count = skuidArray.Count() + 1;//如果没有,就往字典(dict)添加一个新的}string countStr = "";foreach (var v in dict)//再迭代字典拼字符串{countStr += v.Key + "-" + v.Value + ",";//键值对}cookies = new HttpCookie(countStr){Expires = DateTime.Today.AddDays(1),Value = countStr};//拼好了添加到CookieHttpContext.Current.Request.Cookies.Add(cookies);if (userID != null)//如果用户登录了{SkuShoppingCart sc = new SkuShoppingCart();//初始化购物车类sc.GoodsSkuID = gs.skuID;//完成之后给类的属性赋值sc.ShoppingCartID = db.ShoppingCarts//根据用户ID来找相对应的购物车ID.Where(f => f.UserID == userID).Select(f => f.ShoppingCartID).Single();sc.SkuCount = gs.Count;//统计数量db.SkuShoppingCarts.Add(sc);//添加进数据库}return count;//数量#endregion}}}

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表