public async Task <IActionResult> Create ([Bind("WithdrawId,WithdrawDate,Amount,AtmId,UserId")] WithdrawViewModel withdraw) { if (ModelState.IsValid) { var model = withdraw.ToModel(_context); bool hasError = model.AutomatedTellerMachine == null || model.User == null; try { model.CurrencyDictionary = WithdrawHelper.WithdrawFromAtm(model.User, model.AutomatedTellerMachine, withdraw.Amount, _context); } catch (InsufficentFundsException) { ModelState.AddModelError("UserId", $"User {model.User.Name} has insufficient funds (${model.User.Balance}) to withdraw ${withdraw.Amount}"); hasError = true; } catch (InsufficientChangeException) { ModelState.AddModelError("AtmId", $"ATM {model.AutomatedTellerMachine.Alias} has insufficient change to withdraw ${withdraw.Amount}"); hasError = true; } catch { throw; } if (model.AutomatedTellerMachine == null) { ModelState.AddModelError("AtmId", "ATM not found"); } if (model.User == null) { ModelState.AddModelError("UserId", "User not found"); } if (hasError) { ViewBag.Users = await GetUsers(); ViewBag.Atms = await GetAtms(); return(View(withdraw)); } model.WithdrawDate = DateTime.Now; _context.Add(model); await _context.SaveChangesAsync(); (TempData["SuccessMessage"], TempData["InfoMessages"]) = BuildWithdrawMessage(model); return(RedirectToAction("Index")); } return(View(withdraw)); }