public async Task <ActionResult> AddMaterialToOrderProductDelivery(int?orderId, int?productId, int?deliveryId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (deliveryId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries .Include(opd => opd.OrderProduct.Order) .Include(opd => opd.OrderProduct.Product) .FirstOrDefaultAsync(opa => opa.OrderId == orderId && opa.ProductId == productId && opa.DeliveryId == deliveryId); if (orderProductDelivery == null) { return(HttpNotFound()); } AddMaterialToOrderProductDeliveryViewModel model = new AddMaterialToOrderProductDeliveryViewModel(); model.OrderProductDelivery = new OrderProductDeliveryViewModel(orderProductDelivery); model.AvailableMaterials = GetAvailableMaterials(orderProductDelivery); return(View(model)); }
public async Task <ActionResult> AddMaterialToOrderProductDelivery(AddMaterialToOrderProductDeliveryViewModel model) { OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries .FindAsync(model.OrderProductDelivery.OrderProduct.Order.Id, model.OrderProductDelivery.OrderProduct.Product.Id, model.OrderProductDelivery.DeliveryId); if (orderProductDelivery == null) { return(HttpNotFound()); } if (!ModelState.IsValid) { model.AvailableMaterials = GetAvailableMaterials(orderProductDelivery, model.MaterialId); return(View(model)); } Material material = await context.Materials.FindAsync(model.MaterialId); if (material == null) { return(HttpNotFound()); } context.OrderProductDeliveryMaterials.Add(new OrderProductDeliveryMaterial { OrderProductDelivery = orderProductDelivery, Material = material, DeliveryQuantity = model.DeliveryQuantity, AcceptanceQuantity = model.AcceptanceQuantity }); 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 = orderProductDelivery.OrderId, productId = orderProductDelivery.ProductId, deliveryId = orderProductDelivery.DeliveryId })); }