public async Task <ActionResult> DeleteDeliveryFromOrderProduct(DeleteDeliveryFromOrderProductViewModel model) { OrderProductDelivery orderProductDelivery = await context.OrderProductDeliveries .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.DeliveryId); if (orderProductDelivery == null) { return(HttpNotFound()); } context.OrderProductDeliveries.Remove(orderProductDelivery); 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 = orderProductDelivery.OrderId, productId = orderProductDelivery.ProductId })); }
public async Task <ActionResult> DeleteDeliveryFromOrderProduct(int?orderId, int?productId, int?deliveryId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (deliveryId == 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()); } if (!context.OrderProductDeliveries.Any(opd => opd.DeliveryId == deliveryId)) { return(HttpNotFound()); } DeleteDeliveryFromOrderProductViewModel model = new DeleteDeliveryFromOrderProductViewModel(orderProduct, deliveryId.Value); return(View(model)); }