public async Task <IActionResult> Buy(int id) { Product productModel = new Product(); if (CartSessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart") == null) { List <Item> cart = new List <Item>(); cart.Add(new Item { Product = await _context.Products.FindAsync(id), Quantity = 1 }); CartSessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } else { List <Item> cart = CartSessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); int index = isExisting(id); if (index != -1) { cart[index].Quantity++; } else { cart.Add(new Item { Product = await _context.Products.FindAsync(id), Quantity = 1 }); } CartSessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } return(RedirectToAction("Cart")); }
public IActionResult Delete(int id) { List <Item> cart = CartSessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart"); int index = isExisting(id); cart.RemoveAt(index); CartSessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); return(RedirectToAction("cart")); }