public async Task <ActionResult> DeleteMaterialFromOrderProductDelivery(int?orderId, int?productId, int?deliveryId, int?materialId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

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

            OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries
                                                        .Include(opd => opd.OrderProduct.Order)
                                                        .Include(opd => opd.OrderProduct.Product)
                                                        .Include(opd => opd.OrderProductDeliveryMaterials)
                                                        .FirstOrDefaultAsync(opd => opd.OrderId == orderId &&
                                                                             opd.ProductId == productId &&
                                                                             opd.DeliveryId == deliveryId);

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

            Material material = await context.Materials.FindAsync(materialId);

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

            DeleteMaterialFromOrderProductDeliveryViewModel model = new DeleteMaterialFromOrderProductDeliveryViewModel(orderProductDelivery, material);

            return(View(model));
        }
        public async Task <ActionResult> DeleteMaterialFromOrderProductDelivery(DeleteMaterialFromOrderProductDeliveryViewModel model)
        {
            OrderProductDeliveryMaterial orderProductDeliveryMaterial = await context.OrderProductDeliveryMaterials
                                                                        .FindAsync(model.OrderProductDelivery.OrderProduct.Order.Id,
                                                                                   model.OrderProductDelivery.OrderProduct.Product.Id,
                                                                                   model.OrderProductDelivery.DeliveryId,
                                                                                   model.Material.Id);

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

            context.OrderProductDeliveryMaterials.Remove(orderProductDeliveryMaterial);

            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 = orderProductDeliveryMaterial.OrderId,
                productId = orderProductDeliveryMaterial.ProductId,
                deliveryId = orderProductDeliveryMaterial.DeliveryId
            }));
        }