public RedirectToRouteResult ThemSoSanh(string SP_ID) { if (Session["sosanh"] == null) // Nếu sản phẩm chưa được khởi tạo { Session["sosanh"] = new List <SoSanhSP>(); // Khởi tạo Session là 1 List<SoSanh> } List <SoSanhSP> sosanh = Session["sosanh"] as List <SoSanhSP>; // Gán qua biến sosanh dễ code // Kiểm tra xem sản phẩm khách đang chọn đã có trong giỏ hàng chưa if (sosanh.FirstOrDefault(m => m.SP_ID == SP_ID) == null) // ko co sp nay trong gio hang { if (sosanh.Count >= 2) { sosanh.Remove(sosanh.LastOrDefault()); } SANPHAM sp = db.SANPHAMs.Find(SP_ID); // tim sp theo id SoSanhSP ss = new SoSanhSP() { SP_ID = SP_ID, }; // Tạo ra 1 CartItem mới sosanh.Add(ss); // Thêm CartItem vào giỏ } // Action này sẽ chuyển hướng về trang chi tiết sp khi khách hàng đặt vào giỏ thành công. Bạn có thể chuyển về chính trang khách hàng vừa đứng bằng lệnh return Redirect(Request.UrlReferrer.ToString()); nếu muốn. return(RedirectToAction("SearchSP")); }
public RedirectToRouteResult XoaDoiTuong(string SP_ID) { List <SoSanhSP> sosanh = Session["sosanh"] as List <SoSanhSP>; SoSanhSP itemXoa = sosanh.FirstOrDefault(m => m.SP_ID == SP_ID); if (itemXoa != null) { sosanh.Remove(itemXoa); } return(RedirectToAction("SearchSP")); }