ModelState验证不起作用ASP.NET MVC
我是ASP.NET MVC的新手,我决定构建一个无用于学习目的数据库的ATM Web应用程序。我正在弄清楚MVC模式,在大多数情况下,我使它有效,但是如果输入错误的数据,我需要帮助验证输入的提款金额并显示正确的错误消息。谢谢。
另外,我检查用户是否完成的交易的逻辑。 ''''emp.transactionbal< = 10'''''但是,这种情况一直降低到0,-1,-2等。但是我希望它停在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
<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>
模型类 提取
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; }
}
I'm new to asp.net MVC, I decided to build an ATM web APP without any database for learning purposes. 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. 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. But I want it to stop at 0. Thanks
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
<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
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果条件失败,请尝试添加错误。这样的东西。
try adding error when your if condition is failing. something like this.