public void AddToCart(Product product) { // Get the matching cart and product instances var cartItem = _dbContext.CartItems.SingleOrDefault( c => c.CartId == Id && c.ProductId == product.Id); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new CartItem { Product = product, CartId = Id, Count = 1, DateCreated = DateTime.Now }; _dbContext.CartItems.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } }
public async Task<IActionResult> Edit(Product product) { if (ModelState.IsValid) { db.Entry(product).SetState(EntityState.Modified); await db.SaveChangesAsync(Context.RequestAborted); return RedirectToAction("Index"); } return View(product); }
public async Task<IActionResult> Create(Product product) { if (ModelState.IsValid) { await db.Products.AddAsync(product, Context.RequestAborted); await db.SaveChangesAsync(Context.RequestAborted); return RedirectToAction("Index"); } return View(product); }