public async Task <ActionResult> Edit(int?orderId, int?productId, int?addressId, int?deliveryId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (addressId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (deliveryId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductAddressDelivery orderProductAddressDelivery = await context.OrderProductAddressDeliveries .Include(opad => opad.OrderProductAddress.OrderProduct.Order) .Include(opad => opad.OrderProductAddress.OrderProduct.Product) .Include(opad => opad.OrderProductAddress.Address) .Include(opad => opad.OrderProductAddressDeliverySizes) .Include(opad => opad.OrderProductAddressDeliverySizes.Select(opads => opads.Size)) .FirstOrDefaultAsync(opad => opad.OrderId == orderId && opad.ProductId == productId && opad.AddressId == addressId && opad.DeliveryId == deliveryId); if (orderProductAddressDelivery == null) { return(HttpNotFound()); } EditOrderProductAddressDeliveryViewModel model = new EditOrderProductAddressDeliveryViewModel(); model.OrderProductAddress = new OrderProductAddressViewModel(orderProductAddressDelivery.OrderProductAddress); model.DeliveryId = orderProductAddressDelivery.DeliveryId; model.DeliveryDate = orderProductAddressDelivery.DeliveryDate; model.DeliveryImagePath = orderProductAddressDelivery.DeliveryImagePath; model.AcceptanceId = orderProductAddressDelivery.AcceptanceId; model.AcceptanceDate = orderProductAddressDelivery.AcceptanceDate; model.AcceptanceImagePath = orderProductAddressDelivery.AcceptanceImagePath; if (orderProductAddressDelivery.OrderProductAddressDeliverySizes.Any()) { foreach (var orderProductAddressDeliverySize in orderProductAddressDelivery.OrderProductAddressDeliverySizes) { model.OrderProductAddressDeliverySizes.Add(new OrderProductAddressDeliverySizeViewModel(orderProductAddressDeliverySize)); } } return(View(model)); }
public async Task <ActionResult> Edit(EditOrderProductAddressDeliveryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } OrderProductAddressDelivery orderProductAddressDelivery = await context.OrderProductAddressDeliveries .FindAsync(model.OrderProductAddress.OrderProduct.Order.Id, model.OrderProductAddress.OrderProduct.Product.Id, model.OrderProductAddress.Address.Id, model.DeliveryId); if (orderProductAddressDelivery == null) { return(HttpNotFound()); } orderProductAddressDelivery.DeliveryDate = model.DeliveryDate; orderProductAddressDelivery.DeliveryImagePath = model.DeliveryImagePath; orderProductAddressDelivery.AcceptanceId = model.AcceptanceId; orderProductAddressDelivery.AcceptanceDate = model.AcceptanceDate; orderProductAddressDelivery.AcceptanceImagePath = model.AcceptanceImagePath; context.Entry(orderProductAddressDelivery).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 = orderProductAddressDelivery.OrderId, productId = orderProductAddressDelivery.ProductId, addressId = orderProductAddressDelivery.AddressId, deliveryId = orderProductAddressDelivery.DeliveryId })); }