public async Task <IActionResult> CloseOrder(int id, OrderCloseViewModel viewModel) { var currentOrder = _context.Order.Include(o => o.OrderProducts).FirstOrDefault(o => o.OrderId == id); var OrderProducts = _context.OrderProduct.Include(o => o.Product).Where(o => o.OrderId == id).ToList(); currentOrder.DateCompleted = DateTime.Now; currentOrder.PaymentTypeId = viewModel.Order.PaymentTypeId; //remove from quantity and update DB foreach (var item in OrderProducts) { item.Product.Quantity = item.Product.Quantity - 1; _context.Update(item.Product); } ModelState.Remove("Order.UserId"); ModelState.Remove("Order.User"); if (ModelState.IsValid) { _context.Update(currentOrder); await _context.SaveChangesAsync(); return(RedirectToAction("ConfirmationPage", "Orders", new { id = id }));; } return(View(viewModel)); }
// GET: Orders/CompletePayment/2 public async Task <IActionResult> CloseOrder(int id) { var user = await _userManager.GetUserAsync(HttpContext.User); var order = _context.Order.FirstOrDefault(o => o.OrderId == id); var OrderProducts = _context.OrderProduct.Include(o => o.Product).Where(o => o.OrderId == id).ToList(); var paymentTypes = _context.PaymentType.Where(p => p.UserId == user.Id).ToList(); var viewModel = new OrderCloseViewModel(); viewModel.PaymentTypes = paymentTypes.Select(a => new SelectListItem { Value = a.PaymentTypeId.ToString(), Text = a.AccountNumber }).ToList(); viewModel.Order = order; viewModel.Order.OrderProducts = OrderProducts; return(View(viewModel)); }