ModelState验证不起作用ASP.NET MVC

发布于 2025-02-10 01:37:42 字数 3387 浏览 1 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2025-02-17 01:37:42

如果条件失败,请尝试添加错误。这样的东西。

    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);
                }
    }

try adding error when your if condition is failing. 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);
                }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文