public async Task <IActionResult> Edit(int id, [Bind("ProductID,Title,Category,Size,Price,UnitsInStock")] Product product) { if (id != product.ID) { return(NotFound()); } if (ModelState.IsValid) { try { context.Update(product); await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(product.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Edit(int id, [Bind("CustomerID,FirstName,LastName,Email,Gender,Street,City,State,Country,Postcode,PhoneNumber")] Customer customer) { if (id != customer.ID) { return(NotFound()); } if (ModelState.IsValid) { try { context.Update(customer); await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(customer.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <IActionResult> Edit(int id, [Bind("ID,OrderNumber,OrderStatus,OrderDate,CustomerID")] Order order, string[] selectedProducts, int[] quantity) { if (selectedProducts != null && quantity != null) { try { context.RemoveRange(context.OrderItems.Where(i => i.OrderID == order.ID).ToList()); order.OrderItems = new List <OrderItem>(); quantity = quantity.Where(val => val != 0).ToArray(); int index = 0; foreach (var product in selectedProducts) { var prod = await context.Products .SingleOrDefaultAsync(i => i.ID == int.Parse(product)); var orderItemToAdd = new OrderItem { Product = prod, Quantity = quantity[index] }; order.OrderItems.Add(orderItemToAdd); index++; } decimal sum = 0.0M; foreach (var item in order.OrderItems) { sum += Convert.ToDecimal(item.Quantity) * item.Product.Price; } order.Total = sum; foreach (var item in order.OrderItems) { var product = await context.Products .SingleOrDefaultAsync(m => m.ID == item.Product.ID); if (product.UnitsInStock - item.Quantity < 0) { ModelState.AddModelError("", "There are not enough products of type: " + product.Title); } else { product.UnitsInStock -= item.Quantity; context.Products.Update(product); } } var customer = await context.Customers .SingleOrDefaultAsync(m => m.ID == order.CustomerID); mailService.SendMail(order, customer, order.OrderStatus); } catch (DbUpdateConcurrencyException) { if (!OrderExists(order.ID)) { return(NotFound()); } else { throw; } } } if (ModelState.IsValid) { context.Update(order); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedProducts(order); ViewData["CustomerID"] = new SelectList(context.Customers, "ID", "FullName", order.CustomerID); return(View(order)); }