public async Task <IActionResult> Edit(int id, ProductCreationalViewModel productVM) { if (id != productVM.Product.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { var currentProductInstanceInDatabase = await _context.Products.Include(p => p.Price).Include(c => c.Price.Currency).AsNoTracking().FirstOrDefaultAsync(p => p.ProductId == id); if (currentProductInstanceInDatabase.Price.Amount != productVM.Price.Amount || productVM.Price.CurrencyId != currentProductInstanceInDatabase.Price.CurrencyId) { var newProductCurrency = await _context.Currencies.Where(p => p.CurrencyId == productVM.Price.CurrencyId).Select(c => c.CurrencyUnit).AsNoTracking().SingleOrDefaultAsync(); productVM.Price.Description = $"{currentProductInstanceInDatabase.ProductName}'s price has changed from {currentProductInstanceInDatabase.Price.Amount} {currentProductInstanceInDatabase.Price.Currency.CurrencyUnit} To {productVM.Price.Amount} {newProductCurrency}"; productVM.Price.CreatedDate = DateTime.Today; productVM.Price.EffectiveDate = DateTime.Today; _context.Update(productVM.Price); await _context.SaveChangesAsync(); } _context.Update(productVM.Product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductsExists(productVM.Product.ProductId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["BrandId"] = new SelectList(_context.Brands, "BrandId", "BrandName", productVM.Product.BrandId); ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "CategoryName", productVM.Product.CategoryId); ViewData["Currencies"] = new SelectList(_context.Currencies, "CurrencyId", "CurrencyUnit", productVM.Price.CurrencyId); return(View(productVM)); }
public async Task <IActionResult> Edit(int id, [Bind("BrandId,BrandName")] Brands brands) { if (id != brands.BrandId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(brands); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BrandsExists(brands.BrandId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(brands)); }
public async Task <IActionResult> Edit(int id, [Bind("CategoryId,CategoryName")] Categories categories) { if (id != categories.CategoryId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(categories); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoriesExists(categories.CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(categories)); }
public async Task <IActionResult> Edit(int id, int productId, [Bind("StoreId,ProductId,Quantity")] Stocks stocks) { if (id != stocks.StoreId || productId != stocks.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(stocks); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StocksExists(stocks.StoreId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "ProductName", stocks.ProductId); ViewData["StoreId"] = new SelectList(_context.Stores, "StoreId", "StoreName", stocks.StoreId); return(View(stocks)); }
public async Task <IActionResult> Edit(int id, [Bind("CurrencyId,CurrencyUnit,CreatedDate")] Currency currency) { if (id != currency.CurrencyId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(currency); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CurrencyExists(currency.CurrencyId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(currency)); }