Ejemplo n.º 1
0
        public JsonResult AddToCart(int id)
        {
            List <CartItem> giohang = null;

            if (Session["giohang"] == null) // Giỏ hàng trống
            {
                giohang = new List <CartItem>();
                giohang.Add(new CartItem()
                {
                    ProductOrder = sachRepo.GetByID(id), Quantity = 1
                });
            }
            else // Giỏ hàng đã có sản phẩm
            {
                giohang = (List <CartItem>)Session["giohang"];
                CartItem s = giohang.SingleOrDefault(x => x.ProductOrder.Masach == id);
                if (s != null)
                {
                    s.Quantity++; // Tăng số lượng thêm 1
                }
                else
                {
                    giohang.Add(new CartItem()
                    {
                        ProductOrder = sachRepo.GetByID(id), Quantity = 1
                    });
                }
            }

            // Cập nhật Session["giohang"]
            Session["giohang"] = giohang;
            return(Json(new { ItemAmount = giohang.Sum(x => x.Quantity) }));
        }
Ejemplo n.º 2
0
 public ActionResult Details(int id)
 {
     return(View(sachRepo.GetByID(id)));
 }