public async Task <ActionResult> Edit(EditOrderProductDeliveryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.DeliveryId); if (orderProductDelivery == null) { return(HttpNotFound()); } orderProductDelivery.DeliveryDate = model.DeliveryDate; orderProductDelivery.DeliveryImagePath = model.DeliveryImagePath; orderProductDelivery.AcceptanceId = model.AcceptanceId; orderProductDelivery.AcceptanceDate = model.AcceptanceDate; orderProductDelivery.AcceptanceImagePath = model.AcceptanceImagePath; context.Entry(orderProductDelivery).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 = orderProductDelivery.OrderId, productId = orderProductDelivery.ProductId, deliveryId = orderProductDelivery.DeliveryId })); }
public async Task <ActionResult> Edit(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) .Include(opd => opd.OrderProductDeliveryMaterials.Select(opdm => opdm.Material)) .FirstOrDefaultAsync(opd => opd.OrderId == orderId && opd.ProductId == productId && opd.DeliveryId == deliveryId); if (orderProductDelivery == null) { return(HttpNotFound()); } EditOrderProductDeliveryViewModel model = new EditOrderProductDeliveryViewModel(); model.OrderProduct = new OrderProductViewModel(orderProductDelivery.OrderProduct); model.DeliveryId = orderProductDelivery.DeliveryId; model.DeliveryDate = orderProductDelivery.DeliveryDate; model.DeliveryImagePath = orderProductDelivery.DeliveryImagePath; model.AcceptanceId = orderProductDelivery.AcceptanceId; model.AcceptanceDate = orderProductDelivery.AcceptanceDate; model.AcceptanceImagePath = orderProductDelivery.AcceptanceImagePath; if (orderProductDelivery.OrderProductDeliveryMaterials.Any()) { foreach (var orderProductlDeliveryMaterial in orderProductDelivery.OrderProductDeliveryMaterials) { model.OrderProductDeliveryMaterials.Add(new OrderProductDeliveryMaterialViewModel(orderProductlDeliveryMaterial)); } } return(View(model)); }