[英]ModelState Validation not working asp.net MVC
I'm new to asp.net MVC, I decided to build an ATM web APP without any database for learning purposes.我是asp.net MVC的新手,我决定构建一个没有任何数据库的ATM web APP用于学习目的。 I'm figuring out the MVC pattern, for the most part, I got it working but I need help with validating the entered withdrawal amount and displaying the correct error message if incorrect data is entered.我正在弄清楚 MVC 模式,在大多数情况下,我得到了它的工作,但我需要帮助来验证输入的提款金额并在输入不正确的数据时显示正确的错误消息。 Thanks.谢谢。
Also, the logic where I check if the transactions the user has completed.此外,我检查用户是否已完成交易的逻辑。 ''' emp.TransactionBal <= 10 ''' but the condition keeps going down to 0, - 1, -2 and so on. ''' emp.TransactionBal <= 10 ''' 但条件一直下降到 0、- 1、-2 等等。 But I want it to stop at 0. Thanks但我希望它停止在 0。谢谢
public class WithdrawController : Controller
{
WithdrawRepository rep = new WithdrawRepository();
[BindProperty]
public InputModel Input { get; set; }
//
// GET: /Withdraw/
public ActionResult Index()
{
IEnumerable<Withdraw> obj = rep.SelectAllWithdraws();
return View(obj);
}
// GET: /Withdraw/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Withdraw/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Withdraw emp)
{
foreach (var obj in rep.SelectAllWithdraws())
{
emp.WithdrawId = obj.WithdrawId;
emp.WithdrawDate = DateTime.Now;
emp.TransactionBal = obj.TransactionBal;
emp.AccountBal = obj.AccountBal;
emp.User= obj.User;
emp.UserID = obj.UserID;
}
try
{
//Check if user have enough cash for withdraw
//Check if the transaction is not more than 1000
//Check if the user have not exceeded 10 transaction that day
if (ModelState.IsValid && (emp.AccountBal - Input.WithdrawAmt) > 0)
{
if (Input.WithdrawAmt <= 1000 && emp.TransactionBal <= 10)
{
emp.WithdrawId++;
emp.TransactionBal--;
emp.AccountBal -= Input.WithdrawAmt;
rep.Add(emp);
return RedirectToAction("Index");
}
}
}
catch
{
ModelState.AddModelError("", "Unable to complete the transaction. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
return View();
}
public class InputModel: Withdraw
{
}
Create.cshtml创建.cshtml
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="mb-3">
<label asp-for="WithdrawAmt">Amount</label>
<input asp-for="WithdrawAmt" class="form-control" />
<span asp-validation-for="WithdrawAmt" class = "text-danger"></span>
</div>
<button class="btn btn-success">Create</button>
</form>
</div>
</div>
Model Class Withdraw.cs模型类 Withdraw.cs
public class Withdraw
{
public int WithdrawId { get; set; }
[Required]
public double WithdrawAmt { get; set; }
public int TransactionBal { get; set; }
public DateTime WithdrawDate { get; set; }
public double AccountBal { get; set; }
}
try adding error when your if condition is failing.当您的 if 条件失败时尝试添加错误。 something like this.像这样的东西。
try
{
//Check if user have enough cash for withdraw
//Check if the transaction is not more than 1000
//Check if the user have not exceeded 10 transaction that day
if (ModelState.IsValid && (emp.AccountBal - Input.WithdrawAmt) > 0)
{
if (Input.WithdrawAmt <= 1000 && emp.TransactionBal <= 10)
{
emp.WithdrawId++;
emp.TransactionBal--;
emp.AccountBal -= Input.WithdrawAmt;
rep.Add(emp);
return RedirectToAction("Index");
}
}
else
{
ModelState.AddModelError("WithdrawAmt","Not enough balance")
return View(emp);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.