简体   繁体   English

ASP.NET CORE - 如果 TextBoxFor 中的值为 false,如何抛出错误?

[英]ASP.NET CORE - How to throw an error if value in TextBoxFor is false?

I am making a simple login page.我正在制作一个简单的登录页面。 I keep Username and Password in MS SQL.我将Username名和Password保存在 MS SQL 中。 By comparing the Username and Password entered in the TextBox For the user to the values in the database, I direct them to the home page or to the login page again.通过将在TextBox For user 中输入的UsernamePassword与数据库中的值进行比较,我将它们引导到主页或再次登录页面。

My codes:我的代码:

Login/Login.cshtml : Login/Login.cshtml

@model ESchool.Models.ManagerAccount
@{
    ViewData["Title"] = "Home Page";
}
<div class="p-3 mb-2 bg-dark text-white">Ad</div>
<div class="card">
    <article class="card-body">
        <h4 class="card-title mb-4 mt-1">Sign in</h4>
        <form action="/Account/Login" method="post">
            <div class="form-group">
                <label>Username:</label>
                @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
            </div>
            <div class="form-group">
                <label>Password:</label>
                @Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-block">Login</button>
            </div>
        </form>
    </article>
</div>

Controller: Controller:

using ESchool.Models;
using Microsoft.AspNetCore.Mvc;

namespace ESchool.Controllers
{
    public class AccountController : Controller
    {
        SchoolContext db = new SchoolContext();
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Login(ManagerAccount m)
        {
            string username = m.Username;
            System.Diagnostics.Debug.WriteLine(username);
            string password = m.Password;
            System.Diagnostics.Debug.WriteLine(password);
            var getUser = db.ManagerAccounts.SingleOrDefault(i => i.Username == m.Username);
            if (getUser != null)
            {
                System.Diagnostics.Debug.WriteLine("username is correct.");
                var getPassword = getUser.Password;
                System.Diagnostics.Debug.WriteLine("get: " + getPassword);
                if (getPassword == m.Password)
                {
                    System.Diagnostics.Debug.WriteLine("password is correct.");
                    return View("~/Views/Home/Index.cshtml");
                }
                else
                {
                    // Give error
                    return View("Login");
                }
            }
            else
            {
                // Give error
                return View("Login");
            }
        }
    }
}

Model: Model:

using System;
using System.Collections.Generic;

namespace ESchool.Models
{
    public partial class ManagerAccount
    {
        public int Id { get; set; }
        public string? Username { get; set; }
        public string? Email { get; set; }
        public string? Address { get; set; }
        public string? Password { get; set; }
        public string? ProfilePhoto { get; set; }
        public string? SecurityQuestionAnswer { get; set; }
    }
}

What I want to do is show an error to the user on the Login.cshtml page if the Username and Password are wrong.如果Username名和Password错误,我想要做的是在Login.cshtml页面上向用户显示错误。 How can I do that?我怎样才能做到这一点? Thanks for helpful.感谢您的帮助。

You can added custom validations using ModelState.AddModelError .您可以使用ModelState.AddModelError添加自定义验证。

Controller: Controller:

if(getUser == null)
{
    ModelState.AddModelError("Username", "User or password are not correct");
    return View();
}

View:看法:

 <span asp-validation-for="Username" class="text-danger"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM