/*public ActionResult dIndex() * { * var cart = Session[CartSession]; * var list = new List<CartItem>(); * if (cart != null) * { * list = (List<CartItem>)cart; * } * return View(list); * }*/ public ActionResult AddItem(int sachID, int Quantity) { var sanpham = new CartItemFunction().ViewDetail(sachID); var cart = (Cart)Session[CartSession]; if (cart != null) { cart.AddItem(sanpham, 1); Session[CartSession] = cart; } else { cart = new Cart(); cart.AddItem(sanpham, 1); Session[CartSession] = cart; } return(RedirectToAction("Index", "Cart")); }
/*public ActionResult dIndex() * { * var cart = Session[CartSession]; * var list = new List<CartItem>(); * if (cart != null) * { * list = (List<CartItem>)cart; * } * return View(list); * }*/ public ActionResult AddItem(int sachID, int Quantity) { var sanpham = new CartItemFunction().ViewDetail(sachID); var cart = Session[CartSession]; if (cart != null) { var list = (List <CartItemModel>)cart; if (list.Exists(x => x.sach.SachID == sachID)) { foreach (var item in list) { if (item.sach.SachID == sachID) { item.Quantity += Quantity; } } } else { var item = new CartItemModel(); item.sach = sanpham; item.Quantity = Quantity; list.Add(item); } Session[CartSession] = list; } else { var item = new CartItemModel(); item.sach = sanpham; item.Quantity = Quantity; var list = new List <CartItemModel>(); list.Add(item); //gan vao sesion Session[CartSession] = list; } return(RedirectToAction("Index", "Cart")); }