public async Task <ActionResult> DeleteAddressFromOrderProduct(DeleteAddressFromOrderProductViewModel model) { OrderProductAddress orderProductAddress = await context.OrderProductAddresses .FindAsync(model.OrderProduct.Order.Id, model.OrderProduct.Product.Id, model.Address.Id); if (orderProductAddress == null) { return(HttpNotFound()); } context.OrderProductAddresses.Remove(orderProductAddress); 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 = orderProductAddress.OrderId, productId = orderProductAddress.ProductId })); }
public async Task <ActionResult> DeleteAddressFromOrderProduct(int?orderId, int?productId, int?addressId) { if (orderId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (addressId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } OrderProduct orderProduct = await context.OrderProducts .Include(op => op.Order) .Include(op => op.Product) .FirstOrDefaultAsync(oa => oa.OrderId == orderId && oa.ProductId == productId); if (orderProduct == null) { return(HttpNotFound()); } Address address = await context.Addresses.FindAsync(addressId); if (address == null) { return(HttpNotFound()); } DeleteAddressFromOrderProductViewModel model = new DeleteAddressFromOrderProductViewModel(orderProduct, address); return(View(model)); }