public async Task <ActionResult> Edit(int?orderId, int?productId, int?addressId, DateTime?productionDate) { 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 (productionDate == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates .Include(opapd => opapd.OrderProductAddress.OrderProduct.Order) .Include(opapd => opapd.OrderProductAddress.OrderProduct.Product) .Include(opapd => opapd.OrderProductAddress.Address) .Include(opapd => opapd.OrderProductAddressProductionDateSizes) .Include(opapd => opapd.OrderProductAddressProductionDateSizes.Select(opapds => opapds.Size)) .FirstOrDefaultAsync(opapd => opapd.OrderId == orderId && opapd.ProductId == productId && opapd.AddressId == addressId && opapd.ProductionDate == productionDate); if (orderProductAddressProductionDate == null) { return(HttpNotFound()); } EditOrderProductAddressProductionDateViewModel model = new EditOrderProductAddressProductionDateViewModel(); model.OrderProductAddress = new OrderProductAddressViewModel(orderProductAddressProductionDate.OrderProductAddress); model.ProductionDate = orderProductAddressProductionDate.ProductionDate; if (orderProductAddressProductionDate.OrderProductAddressProductionDateSizes.Any()) { foreach (var orderProductAddressProductionDateSize in orderProductAddressProductionDate.OrderProductAddressProductionDateSizes) { model.OrderProductAddressProductionDateSizes.Add(new OrderProductAddressProductionDateSizeViewModel(orderProductAddressProductionDateSize)); } } return(View(model)); }
public async Task <ActionResult> Edit(EditOrderProductAddressProductionDateViewModel model) { if (!ModelState.IsValid) { return(View(model)); } OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates .FindAsync(model.OrderProductAddress.OrderProduct.Order.Id, model.OrderProductAddress.OrderProduct.Product.Id, model.OrderProductAddress.Address.Id, model.ProductionDate); if (orderProductAddressProductionDate == null) { return(HttpNotFound()); } context.Entry(orderProductAddressProductionDate).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 = orderProductAddressProductionDate.OrderId, productId = orderProductAddressProductionDate.ProductId, addressId = orderProductAddressProductionDate.AddressId, productionDate = orderProductAddressProductionDate.ProductionDate })); }