/// <summary> /// 向购物车中添加商品信息。 /// </summary> protected void AddCart() { if (!string.IsNullOrEmpty(Request.QueryString["id"])) { int bookId = 0; if (!int.TryParse(Request.QueryString["id"], out bookId)) { Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("参数错误") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表")); } else { BLL.BookManager bll = new BookShop.BLL.BookManager(); Model.Book model = bll.GetModel(bookId);//根据传递过来的书的编号查找该书. if (model != null) { BLL.CartManager cartBll = new BookShop.BLL.CartManager(); int userId = ((Model.User)Session["user"]).Id; //得到了当前登录用户的编号. Model.Cart cartModel = cartBll.GetModel(model.Id, userId); //根据用户的编号,与书的编号,找出购物车中的商品项. if (cartModel == null) //如果该条件成立,向购物车表中插入一条记录 { Model.Cart ModelCart = new BookShop.Model.Cart(); ModelCart.User = (Model.User)Session["user"]; ModelCart.Book = model; ModelCart.Count = 1; cartBll.Add(ModelCart); } else//更新该商品项的数量 { cartModel.Count = cartModel.Count + 1; cartBll.Update(cartModel); } } else { Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("该书不存在") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表")); } } } }
public void ProcessRequest(HttpContext context) { if (!string.IsNullOrEmpty(context.Request.Form["action"])) { string action = context.Request.Form["action"]; if (action == "change")//更新商品数量 { int pk, bookId, count; if (!int.TryParse(context.Request.Form["pk"], out pk)) { context.Response.Write("no-参数错误!"); return; } if (!int.TryParse(context.Request.Form["count"], out count)) { context.Response.Write("no-参数错误!"); return; } if (!int.TryParse(context.Request.Form["bookId"], out bookId)) { context.Response.Write("no-参数错误!"); return; } BLL.BookManager bookBll = new BookShop.BLL.BookManager(); Model.Book modelBook = bookBll.GetModel(bookId); if (modelBook != null)//看一下该书是否存在 { BLL.CartManager cartBll = new BookShop.BLL.CartManager(); Model.Cart cartModel = cartBll.GetModel(pk); if (cartModel != null)//根据主键查找该购物车中商品项 { cartModel.Count = count; cartBll.Update(cartModel);//完成数量的更新 context.Response.Write("yes"); } else { context.Response.Write("no"); return; } } else { context.Response.Write("no"); } } //删除一条记录 else if (action == "delete") { if (!string.IsNullOrEmpty(context.Request.Form["pk"])) { int pkId = 0; if (!int.TryParse(context.Request.Form["pk"], out pkId)) { context.Response.Write("no"); return; } BLL.CartManager bllCart = new BookShop.BLL.CartManager(); bllCart.Delete(pkId); context.Response.Write("yes"); } } } }