public async Task <IActionResult> Edit(int id, [Bind("CategoryId,Name")] Category category) { if (id != category.CategoryId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(category); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(category.CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Edit(int id, [Bind("ProductId,Name,Description,Price,Photo,CategoryId")] Product product) { if (id != product.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(product.ProductId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Category, "CategoryId", "Name", product.CategoryId); return(View(product)); }
public IActionResult AddToCart(int Quantity, int ProductId) { // identity product price var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId); var price = product.Price; // determine the username; var cartUsername = GetCartUsername(); // check if this user has this product is already in cart. If so, update quantity var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername); if (cartItem == null) { // if product not already in cart, create and save a new Cart object var cart = new Cart { ProductId = ProductId, Quantity = Quantity, Price = price, Username = cartUsername }; _context.Cart.Add(cart); } else { cartItem.Quantity += Quantity; // add the new quantity to the existing quantity _context.Update(cartItem); } _context.SaveChanges(); //show the cart page return(RedirectToAction("Cart")); }