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

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

            OrderProduct orderProduct = await context.OrderProducts
                                        .Include(op => op.Order)
                                        .Include(op => op.Product)
                                        .Include(op => op.OrderProductMaterials)
                                        .FirstOrDefaultAsync(op => op.OrderId == orderId &&
                                                             op.ProductId == productId);

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

            AddMaterialToOrderProductViewModel model = new AddMaterialToOrderProductViewModel();

            model.OrderProduct       = new OrderProductViewModel(orderProduct);
            model.AvailableMaterials = GetAvailableMaterials(orderProduct);
            model.AvailableSuppliers = GetAvailableSuppliers();

            return(View(model));
        }
        public async Task <ActionResult> AddMaterialToOrderProduct(AddMaterialToOrderProductViewModel model)
        {
            OrderProduct orderProduct = await context.OrderProducts
                                        .FindAsync(model.OrderProduct.Order.Id,
                                                   model.OrderProduct.Product.Id);

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

            if (!ModelState.IsValid)
            {
                model.AvailableMaterials = GetAvailableMaterials(orderProduct, model.MaterialId);
                model.AvailableSuppliers = GetAvailableSuppliers(model.SupplierId);

                return(View(model));
            }

            Material material = await context.Materials.FindAsync(model.MaterialId);

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

            Supplier supplier = await context.Suppliers.FindAsync(model.SupplierId);

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

            context.OrderProductMaterials.Add(new OrderProductMaterial
            {
                OrderProduct = orderProduct,
                Material     = material,
                Supplier     = supplier,
                Quantity     = model.Quantity,
                DeliveryDate = model.DeliveryDate,
                Price        = model.Price,
                Rate         = model.Rate
            });

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