public ActionResult Filter(string search) { var products = db.products.Include(p => p.category).ToList(); if (!String.IsNullOrEmpty(search)) { products = products.Where(item => item.category.name.Contains(search) || search == null).ToList(); } var cart = db.carts.ToList(); var Viewmodel = new ProductCarts { carts = cart, products = products }; return(View(Viewmodel)); }
public async Task <IActionResult> RemoveCartsSession(ProductCarts id) { try { List <Item> cart = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); int index = IsExist(Guid.Parse(id.Id)); cart.RemoveAt(index); SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); var carts = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); return(Ok(new Result() { Code = (int)HttpStatusCode.OK, Data = carts, Error = null, Message = null })); } catch (Exception ex) { _logger.LogError("Xóa sản phẩm khỏi giỏ hàng thất bại: " + ex); return(Ok(new Result() { Code = (int)HttpStatusCode.OK, Data = null, Error = "Xóa sản phẩm khỏi giỏ hàng thất bại" })); } }
public async Task <IActionResult> AddToCarts(ProductCarts id) { try { var getSession = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); var ids = new Guid(id.Id); ProductCart products = new ProductCart(); var data = _applicationContext.Products.Where(x => x.Id == ids).Select(x => new { Name = x.Name, Id = x.Id, Cost = x.Cost, Image = _applicationContext.Images.Where(p => p.ProductId == x.Id).Select(img => new { Path = "data:image/png;base64, " + ConvertBase64.GetBase64StringForImage(img.Path) }) }); foreach (var item in data) { products.Id = item.Id; products.Name = item.Name; products.Cost = item.Cost; foreach (var image in item.Image) { products.Path = image.Path; } } if (products == null) { return(Ok(new Result() { Code = (int)HttpStatusCode.OK, Data = "Không tìm thấy sản phẩm", Error = "Không tìm thấy sản phẩm" })); } else { if (getSession == null) { List <Item> cart = new List <Item>(); cart.Add(new Item { ProductCart = products, Quantity = 1 }); SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } else { List <Item> cart = getSession; int index = IsExist(ids); if (index != -1) { cart[index].Quantity++; } else { cart.Add(new Item { ProductCart = products, Quantity = 1 }); } SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } return(Ok(new Result() { Code = (int)HttpStatusCode.OK, Data = "Thêm sản phẩm vào giỏ hàng thành công", Error = null })); } } catch (Exception ex) { _logger.LogError("Thêm sản phẩm vào giỏ hàng thất bại: " + ex); return(Ok(new Result() { Code = (int)HttpStatusCode.OK, Data = null, Error = "Thêm sản phẩm vào giỏ hàng thất bại" })); } }