public async Task <IActionResult> Create([Bind("OrderID,OrderNumber,DateOrdered,CustomerName,CustomerAddress,OrderLines")] Order order) { if (ModelState.IsValid) { _context.Add(order); foreach (var line in order.OrderLines) { line.OrderID = order.OrderID; _context.OrderLines.Add(line); } try { await _context.SaveChangesAsync(); } // Exception for duplicate order IDs catch (DbUpdateException ex) { // send message to view ModelState.AddModelError("OrderID", "An order with this ID already exists"); ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription"); return(View(order)); } return(RedirectToAction(nameof(Index))); } ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription"); return(View(order)); }
public async Task <IActionResult> Create([Bind("BinID,BinName")] Bin bin) { if (ModelState.IsValid) { _context.Add(bin); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { // send message to view ModelState.AddModelError("BinName", "A bin with this name already exists"); return(View(bin)); } return(RedirectToAction(nameof(Index))); } return(View(bin)); }
public async Task <IActionResult> Create([Bind("ProductID,SKU,ProductDescription")] Product product) { if (ModelState.IsValid) { _context.Add(product); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { // send message to view ModelState.AddModelError("SKU", "A product with this SKU already exists"); return(View(product)); } return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("InventoryID,QTY,BinID,ProductID")] Inventory inventory) { if (ModelState.IsValid) { _context.Add(inventory); try { await _context.SaveChangesAsync(); } // Exception for invalid Inventory entry catch (DbUpdateException ex) { // send message to view ModelState.AddModelError(string.Empty, "An inventory for this bin and product already exists"); ViewData["BinID"] = new SelectList(_context.Bins, "BinID", "BinName", inventory.BinID); ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription", inventory.ProductID); return(View(inventory)); } return(RedirectToAction(nameof(Index))); } ViewData["BinID"] = new SelectList(_context.Bins, "BinID", "BinName", inventory.BinID); ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "ProductDescription", inventory.ProductID); return(View(inventory)); }