public async Task <IActionResult> Create([Bind("ProductID,Title,Category,Size,Price,UnitsInStock")] Product product) { if (ModelState.IsValid) { context.Add(product); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("CustomerID,FirstName,LastName,Email,Gender,Street,City,State,Country,Postcode,PhoneNumber")] Customer customer) { if (ModelState.IsValid) { context.Add(customer); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <IActionResult> Create([Bind("ID,OrderNumber,OrderStatus,OrderDate,CustomerID")] Order order, string[] selectedProducts, int[] quantity) { if (selectedProducts != null && quantity != null) { 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++; } order.OrderDate = DateTime.Now; order.OrderNumber = long.Parse(order.OrderDate.Year.ToString() + order.OrderDate.Month.ToString() + order.OrderDate.Day.ToString() + order.OrderDate.Hour.ToString() + order.OrderDate.Minute.ToString() + order.OrderDate.Millisecond.ToString()); order.OrderStatus = "Production"; 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, "Production"); } if (ModelState.IsValid) { context.Add(order); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedProducts(order); ViewData["CustomerID"] = new SelectList(context.Customers, "ID", "FullName", order.CustomerID); return(View(order)); }