Home > OS >  ASP.NET CORE - How to throw an error if value in TextBoxFor is false?
ASP.NET CORE - How to throw an error if value in TextBoxFor is false?

Time:09-21

I am making a simple login page. I keep Username and Password in 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.

My codes:

Login/Login.cshtml:

@model ESchool.Models.ManagerAccount
@{
    ViewData["Title"] = "Home Page";
}
<div >Ad</div>
<div >
    <article >
        <h4 >Sign in</h4>
        <form action="/Account/Login" method="post">
            <div >
                <label>Username:</label>
                @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
            </div>
            <div >
                <label>Password:</label>
                @Html.TextBoxFor(m => m.Password, new { @class = "form-control" })
            </div>
            <div >
                <button type="submit" >Login</button>
            </div>
        </form>
    </article>
</div>

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:

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. How can I do that? Thanks for helpful.

CodePudding user response:

You can added custom validations using ModelState.AddModelError.

Controller:

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

View:

 <span asp-validation-for="Username" ></span>
  • Related