public IActionResult Create(OrderDetail orderDetail) { if (ModelState.IsValid) { _context.OrderDetail.Add(orderDetail); _context.SaveChanges(); return RedirectToAction("Index"); } ViewData["BookID"] = new SelectList(_context.Book, "BookID", "Book", orderDetail.BookID); ViewData["OrderID"] = new SelectList(_context.Set<Order>(), "OrderID", "Order", orderDetail.OrderID); return View(orderDetail); }
public int CreateOrder(Order order) { decimal orderTotal = 0; var cartItems = GetCartItems(); // Iterate over the items in the cart, adding the order details for each foreach (var item in cartItems) { var orderDetail = new OrderDetail { BookId = item.BookId, OrderId = order.Id, UnitPrice = item.Book.Price, Quantity = item.Count }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Book.Price); storeDB.OrderDetails.Add(orderDetail); } // Set the order's total to the orderTotal count order.Total = orderTotal; // Save the order storeDB.SaveChanges(); // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return order.Id; }