public async Task <ActionResult> AddDeliveryToOrderProductAddress(AddDeliveryToOrderProductAddressViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProductAddress orderProductAddress = await context.OrderProductAddresses
                                                      .FindAsync(model.OrderProductAddress.OrderProduct.Order.Id,
                                                                 model.OrderProductAddress.OrderProduct.Product.Id,
                                                                 model.OrderProductAddress.Address.Id);

            if (orderProductAddress == null)
            {
                return(HttpNotFound());
            }

            context.OrderProductAddressDeliveries.Add(new OrderProductAddressDelivery
            {
                OrderProductAddress = orderProductAddress,
                DeliveryId          = model.DeliveryId,
                DeliveryDate        = model.DeliveryDate
            });

            try
            {
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                while (ex != null)
                {
                    errorMessage = ex.Message;
                    ex           = ex.InnerException;
                }

                ModelState.AddModelError("", errorMessage);

                return(View(model));
            }

            return(RedirectToAction(nameof(Details),
                                    new
            {
                orderId = orderProductAddress.OrderId,
                productId = orderProductAddress.ProductId,
                addressId = orderProductAddress.AddressId
            }));
        }
        public async Task <ActionResult> AddDeliveryToOrderProductAddress(int?orderId, int?productId, int?addressId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (addressId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProductAddress orderProductAddress = await context.OrderProductAddresses
                                                      .Include(opa => opa.OrderProduct.Order)
                                                      .Include(opa => opa.OrderProduct.Product)
                                                      .Include(opa => opa.Address)
                                                      .FirstOrDefaultAsync(opa => opa.OrderId == orderId &&
                                                                           opa.ProductId == productId &&
                                                                           opa.AddressId == addressId);

            if (orderProductAddress == null)
            {
                return(HttpNotFound());
            }

            AddDeliveryToOrderProductAddressViewModel model = new AddDeliveryToOrderProductAddressViewModel();

            model.OrderProductAddress = new OrderProductAddressViewModel(orderProductAddress);

            return(View(model));
        }