public async Task <ActionResult> AddAllSizesToOrderProductAddressDelivery(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) .FirstOrDefaultAsync(opad => opad.OrderId == orderId && opad.ProductId == productId && opad.AddressId == addressId && opad.DeliveryId == deliveryId); if (orderProductAddressDelivery == null) { return(HttpNotFound()); } AddAllSizesToOrderProductAddressDeliveryViewModel model = new AddAllSizesToOrderProductAddressDeliveryViewModel(); model.OrderProductAddressDelivery = new OrderProductAddressDeliveryViewModel(orderProductAddressDelivery); model.OrderProductAddressProductionDateSizes = GetOrderProductAddressProductionDateSizeViewModels(orderProductAddressDelivery); return(View(model)); }
public async Task <ActionResult> AddAllSizesToOrderProductAddressDelivery(AddAllSizesToOrderProductAddressDeliveryViewModel model) { 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) .FirstOrDefaultAsync(opad => opad.OrderId == model.OrderProductAddressDelivery.OrderProductAddress.OrderProduct.Order.Id && opad.ProductId == model.OrderProductAddressDelivery.OrderProductAddress.OrderProduct.Product.Id && opad.AddressId == model.OrderProductAddressDelivery.OrderProductAddress.Address.Id && opad.DeliveryId == model.OrderProductAddressDelivery.DeliveryId); if (orderProductAddressDelivery == null) { return(HttpNotFound()); } if (orderProductAddressDelivery.OrderProductAddressDeliverySizes.Any()) { ModelState.AddModelError("", "Поставка готовой продукции грузополучателя заказа уже содержит размерные данные"); } if (!ModelState.IsValid) { model.OrderProductAddressProductionDateSizes = GetOrderProductAddressProductionDateSizeViewModels(orderProductAddressDelivery); return(View(model)); } foreach (var orderProductAddressDeliverySizeViewModel in GetOrderProductAddressProductionDateSizeViewModels(orderProductAddressDelivery)) { Size size = context.Sizes.Find(orderProductAddressDeliverySizeViewModel.Size.Id); if (size != null) { context.OrderProductAddressDeliverySizes.Add(new OrderProductAddressDeliverySize { OrderProductAddressDelivery = orderProductAddressDelivery, Size = size, DeliveryQuantity = orderProductAddressDeliverySizeViewModel.Quantity }); } } 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 })); }