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

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

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

            orderProductAddress.DeliveryDate = model.DeliveryDate;

            context.Entry(orderProductAddress).State = EntityState.Modified;

            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> Edit(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)
                                                      .Include(opa => opa.OrderProductAddressSizes)
                                                      .Include(opa => opa.OrderProductAddressSizes.Select(opas => opas.Size))
                                                      .Include(opa => opa.OrderProductAddressProductionDates)
                                                      .Include(opa => opa.OrderProductAddressDeliveries)
                                                      .FirstOrDefaultAsync(opa => opa.OrderId == orderId &&
                                                                           opa.ProductId == productId &&
                                                                           opa.AddressId == addressId);

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

            EditOrderProductAddressViewModel model = new EditOrderProductAddressViewModel();

            model.OrderProduct = new OrderProductViewModel(orderProductAddress.OrderProduct);
            model.Address      = new AddressViewModel(orderProductAddress.Address);
            model.DeliveryDate = orderProductAddress.DeliveryDate;

            if (orderProductAddress.OrderProductAddressSizes.Any())
            {
                foreach (var orderProductAddressSize in orderProductAddress.OrderProductAddressSizes)
                {
                    model.OrderProductAddressSizes.Add(new OrderProductAddressSizeViewModel(orderProductAddressSize));
                }
            }

            if (orderProductAddress.OrderProductAddressProductionDates.Any())
            {
                foreach (var orderProductAddressProductionDate in orderProductAddress.OrderProductAddressProductionDates)
                {
                    model.OrderProductAddressProductionDates.Add(new OrderProductAddressProductionDateViewModel(orderProductAddressProductionDate));
                }
            }

            if (orderProductAddress.OrderProductAddressDeliveries.Any())
            {
                foreach (var orderProductAddressDelivery in orderProductAddress.OrderProductAddressDeliveries)
                {
                    model.OrderProductAddressDeliveries.Add(new OrderProductAddressDeliveryViewModel(orderProductAddressDelivery));
                }
            }

            return(View(model));
        }