private List <SelectListItem> GetAvailableMaterials(OrderProductDelivery orderProductDelivery, int?materialId = null) { List <SelectListItem> availableMaterials = new List <SelectListItem>(); if (context.Materials.Any()) { foreach (var material in context.Materials.Include(m => m.OrderProductDeliveryMaterials)) { OrderProductDeliveryMaterial orderProductDeliveryMaterial = material.OrderProductDeliveryMaterials .FirstOrDefault(opdm => opdm.OrderId == orderProductDelivery.OrderId && opdm.ProductId == orderProductDelivery.ProductId && opdm.DeliveryId == orderProductDelivery.DeliveryId && opdm.MaterialId == material.Id); if (orderProductDeliveryMaterial == null) { availableMaterials.Add(new SelectListItem { Value = material.Id.ToString(), Text = material.Name, Selected = material.Id == materialId }); } } } return(availableMaterials); }
public OrderProductDeliveryMaterialViewModel(OrderProductDeliveryMaterial orderProductDeliveryMaterial) { OrderProductDelivery = new OrderProductDeliveryViewModel(orderProductDeliveryMaterial.OrderProductDelivery); Material = new MaterialViewModel(orderProductDeliveryMaterial.Material); DeliveryQuantity = orderProductDeliveryMaterial.DeliveryQuantity; AcceptanceQuantity = orderProductDeliveryMaterial.AcceptanceQuantity; }
public async Task <ActionResult> Edit(EditOrderProductDeliveryMaterialViewModel model) { if (!ModelState.IsValid) { return(View(model)); } OrderProductDeliveryMaterial orderProductDeliveryMaterial = await context.OrderProductDeliveryMaterials .FindAsync(model.OrderProductDelivery.OrderProduct.Order.Id, model.OrderProductDelivery.OrderProduct.Product.Id, model.OrderProductDelivery.DeliveryId, model.Material.Id); if (orderProductDeliveryMaterial == null) { return(HttpNotFound()); } orderProductDeliveryMaterial.DeliveryQuantity = model.DeliveryQuantity; orderProductDeliveryMaterial.AcceptanceQuantity = model.AcceptanceQuantity; context.Entry(orderProductDeliveryMaterial).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 = orderProductDeliveryMaterial.OrderId, productId = orderProductDeliveryMaterial.ProductId, deliveryId = orderProductDeliveryMaterial.DeliveryId, materialId = orderProductDeliveryMaterial.MaterialId })); }
public async Task <ActionResult> Edit(int?orderId, int?productId, int?deliveryId, int?materialId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (deliveryId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (materialId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProductDeliveryMaterial orderProductDeliveryMaterial = await context.OrderProductDeliveryMaterials .Include(opdm => opdm.OrderProductDelivery.OrderProduct.Order) .Include(opdm => opdm.OrderProductDelivery.OrderProduct.Product) .Include(opdm => opdm.Material) .FirstOrDefaultAsync(opdm => opdm.OrderId == orderId && opdm.ProductId == productId && opdm.DeliveryId == deliveryId && opdm.MaterialId == materialId); if (orderProductDeliveryMaterial == null) { return(HttpNotFound()); } EditOrderProductDeliveryMaterialViewModel model = new EditOrderProductDeliveryMaterialViewModel(); model.OrderProductDelivery = new OrderProductDeliveryViewModel(orderProductDeliveryMaterial.OrderProductDelivery); model.Material = new MaterialViewModel(orderProductDeliveryMaterial.Material); model.DeliveryQuantity = orderProductDeliveryMaterial.DeliveryQuantity; model.AcceptanceQuantity = orderProductDeliveryMaterial.AcceptanceQuantity; return(View(model)); }
public async Task <ActionResult> DeleteMaterialFromOrderProductDelivery(DeleteMaterialFromOrderProductDeliveryViewModel model) { OrderProductDeliveryMaterial orderProductDeliveryMaterial = await context.OrderProductDeliveryMaterials .FindAsync(model.OrderProductDelivery.OrderProduct.Order.Id, model.OrderProductDelivery.OrderProduct.Product.Id, model.OrderProductDelivery.DeliveryId, model.Material.Id); if (orderProductDeliveryMaterial == null) { return(HttpNotFound()); } context.OrderProductDeliveryMaterials.Remove(orderProductDeliveryMaterial); 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 = orderProductDeliveryMaterial.OrderId, productId = orderProductDeliveryMaterial.ProductId, deliveryId = orderProductDeliveryMaterial.DeliveryId })); }