public async Task <IActionResult> OnPostAsync() { Categories = _context.Categories.Select(n => new SelectListItem { Value = n.Cat_ID.ToString(), Text = n.CategoryName }).ToList(); StockItems = _context.Stock.ToList(); //if (!ModelState.IsValid) //{ // return Page(); //} if (UploadImage != null) { var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "img", UploadImage.FileName); using (var fileStream = new FileStream(fileName, FileMode.Create)) { await UploadImage.CopyToAsync(fileStream); } Product.ImageUrl = "\\img\\" + UploadImage.FileName; // Set the file name } Product.Cat_ID = Convert.ToInt32(SelectedTag); decimal stockPrice = 0; if (StockItemChecked != null) { List <decimal> ssqNums = new List <decimal>(); foreach (string ssq in SelectedStockQuantities) { if (ssq != null) { decimal quan = 0m; quan = decimal.Parse(ssq, CultureInfo.InvariantCulture); ssqNums.Add(quan); } } for (int s = 0; s < StockItemChecked.Count; s++) { StockItem stock = new StockItem(); stock = StockItems.FirstOrDefault(st => st.StockName == StockItemChecked[s]); decimal productStockPrice = stock.Price * ssqNums[s]; stockPrice = stockPrice + productStockPrice; StockToProduct newStockToProduct = new StockToProduct { StockId = stock.Id, ProductId = Product.Product_ID, QuantityUse = ssqNums[s] }; _context.StockToProducts.Add(newStockToProduct); await _context.SaveChangesAsync(); } } Product.CostPrice = stockPrice; Product.ProfitMargin = Product.SellingPrice - stockPrice; _context.Products.Update(Product); await _context.SaveChangesAsync(); TempData["StatusMessage"] = "Product " + Product.ProductName + " successfully created."; return(RedirectToPage("./ProductIndex")); }
public async Task <IActionResult> OnPostAsync() { StockToProducts = _context.StockToProducts.ToList(); //Product = _context.Products.FirstOrDefault(m => m.Product_ID == Product.Product_ID); AllCategories = _context.Categories.ToList(); StockItems = _context.Stock.ToList(); if (!ModelState.IsValid) { return(Page()); } try { string imageurl = Product.ImageUrl; if (UploadImage != null) { var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "img", UploadImage.FileName); using (var fileStream = new FileStream(fileName, FileMode.Create)) { await UploadImage.CopyToAsync(fileStream); } Product.ImageUrl = "\\img\\" + UploadImage.FileName; // Set the file name } else { Product.ImageUrl = imageurl; } Product.Cat_ID = Convert.ToInt32(SelectedTag); decimal stockPrice = 0; if (StockItemChecked != null || StockItemChecked.Count > 0) { List <decimal> ssqNums = new List <decimal>(); foreach (string ssq in SelectedStockQuantities) { if (ssq != null) { decimal quan = 0m; quan = decimal.Parse(ssq, CultureInfo.InvariantCulture); ssqNums.Add(quan); } } for (int s = 0; s < StockItemChecked.Count; s++) { StockItem stock = new StockItem(); stock = StockItems.FirstOrDefault(st => st.StockName == StockItemChecked[s]); decimal qty = ssqNums[s]; decimal productStockPrice = stock.Price * qty; stockPrice = stockPrice + productStockPrice; StockToProduct stp = new StockToProduct(); stp = _context.StockToProducts.FirstOrDefault(st => st.StockId == stock.Id && st.ProductId == Product.Product_ID); if (stp == null) { StockToProduct newStockToProduct = new StockToProduct { StockId = stock.Id, ProductId = Product.Product_ID, QuantityUse = ssqNums[s] }; _context.StockToProducts.Add(newStockToProduct); await _context.SaveChangesAsync(); } else { stp.QuantityUse = ssqNums[s]; _context.StockToProducts.Update(stp); await _context.SaveChangesAsync(); } } } Product.CostPrice = stockPrice; Product.ProfitMargin = Product.SellingPrice - stockPrice; _context.Products.Update(Product); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(Product.Product_ID)) { return(NotFound()); } else { throw; } } TempData["StatusMessage"] = "Product " + Product.ProductName + " successfully edited."; return(RedirectToPage("./ProductIndex")); }