public async Task <ActionResult> DeleteMaterialFromOrderProduct(DeleteMaterialFromOrderProductViewModel model) { OrderProductMaterial orderProductMaterial = await context.OrderProductMaterials .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.Material.Id); if (orderProductMaterial == null) { return(HttpNotFound()); } context.OrderProductMaterials.Remove(orderProductMaterial); 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 = orderProductMaterial.OrderId, productId = orderProductMaterial.ProductId })); }
public async Task <ActionResult> DeleteMaterialFromOrderProduct(int?orderId, int?productId, int?materialId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (materialId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProduct orderProduct = await context.OrderProducts .Include(op => op.Order) .Include(op => op.Product) .FirstOrDefaultAsync(op => op.OrderId == orderId && op.ProductId == productId); if (orderProduct == null) { return(HttpNotFound()); } Material material = await context.Materials.FindAsync(materialId); if (material == null) { return(HttpNotFound()); } DeleteMaterialFromOrderProductViewModel model = new DeleteMaterialFromOrderProductViewModel(orderProduct, material); return(View(model)); }