public IActionResult DatHang(int IdUser, Decimal TotalCost, String DeliveryAddress, DateTime DeliveryDate) { Order order = new Order(); order.IdUser = IdUser; order.TotalCost = TotalCost; order.DeliveryAddress = DeliveryAddress; order.DeliveryDate = DeliveryDate; context.Add(order); context.SaveChanges(); var carts = context.Carts.Include(t => t.IdComboNavigation).Include(t => t.IdProductNavigation).Where(t => t.IdUser == IdUser); Console.WriteLine(order.Id); foreach (var item in carts) { if (item.IdCombo == null) { OrdersDetail ordersDetail = new OrdersDetail() { IdOrders = order.Id , IdCombo = item.IdCombo, IdProduct = item.IdProduct, Amount = item.Amount, Price = item.IdProductNavigation.Price }; var ud = context.Products.Find(ordersDetail.IdProduct); ud.Amount = ud.Amount - ordersDetail.Amount; contextWrite.Add(ordersDetail); contextWrite.SaveChanges(); contextWrite.Update(ud); contextWrite.SaveChanges(); contextWrite.Remove(item); contextWrite.SaveChanges(); } else { OrdersDetail ordersDetail = new OrdersDetail() { IdOrders = order.Id , IdCombo = item.IdCombo, IdProduct = item.IdProduct, Amount = item.Amount, Price = item.IdComboNavigation.Price }; contextWrite.Add(ordersDetail); contextWrite.SaveChanges(); contextWrite.Remove(item); contextWrite.SaveChanges(); } } return(new JsonResult("Đặt hàng thành công")); }