Pass controller error to view

Asked

Viewed 437 times

2

I am making a basic login form and I find myself in the following situation:

I want to return an error if the user does not exist in the database and am doing so using "Modelstate.Addmodelerror".

However, when I error the login I go back to the form, but the error does not appear....

Controller:

public class LoginController : Controller
    {

        private readonly UsuarioService _usuarioService;

        public LoginController(UsuarioService usuarioService)
        {
            _usuarioService = usuarioService;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Logar(Usuario usuario)
        {

            if (!ModelState.IsValid)
            {
                return RedirectToAction(nameof(Index));
            }

            Usuario user = await _usuarioService
                .BuscarUserAsync(x => x.Username == usuario.Username && x.Senha == usuario.Senha);


            //Este é o código que testa o que foi citado acima
            if (user == null)
            {
                ModelState.AddModelError("Aviso", "Usuario ou senha invalidos");
                return RedirectToAction(nameof(Index));
            }

            var claims = new List<Claim>
            {
               new Claim(ClaimTypes.Name, usuario.Username)
            };

            var userIdentity = new ClaimsIdentity(claims, "login");

            ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);

            await HttpContext.SignInAsync(principal);

            return Redirect("/");
        }
    }

View:

@model Site.Models.Usuario;
@{
    ViewData["Title"] = "Login";
}

<h2>@ViewData["Title"]</h2>


<h3>@Html.ValidationMessage("Aviso")</h3>

<div class="row">
    <div class="col-md-6">

        <form asp-action="Logar">
            <div class="form-group">
                <label asp-for="Username" class="control-label"></label>
                <input asp-for="Username" class="form-control" />
            </div>
            <div class="form-group">
                <label asp-for="Senha" class="control-label"></label>
                <input asp-for="Senha" type="password" class="form-control" />
            </div>
            <div class="form-group">
                <input type="submit" value="Logar" class="btn btn-default" />
            </div>
        </form>

    </div>
</div>

1 answer

2


How did you use the RedirectToAction(nameof(Index)); the user will be redirected to Index and Modelstate is recreated, so you don’t have the value you added.

There are some ways to solve, one of them is using Tempdata, so:

Na Action Logar:

//Este é o código que testa o que foi citado acima
 if (user == null)
 {
     //ModelState.AddModelError("Aviso", "Usuario ou senha invalidos");
     TempData["LoginError"] = "Usuario ou senha invalidos";
     return RedirectToAction(nameof(Index));
 }

In the Action Index:

public IActionResult Index()
{
     // Se TempData contém a mensagem de erro, então adiciona no ModelState.
     if (TempData["LoginError"] != null)
     {
           ModelState.AddModelError(string.Empty, TempData["LoginError"].ToString());
     }
     return View();
}

In the View Index:

<div>
    @Html.ValidationSummary(true)
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.