Error while Logging In

Asked

Viewed 56 times

0

I created a View here to log into my system, but in part of ModelState.IsValid just call me back false, I don’t know why, someone could help?

My code of View is as follows:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(Login usuario)
{
    try
    {
        if (ModelState.IsValid)
        {

            var loginViewModel = Mapper.Map<IEnumerable<Login>, IEnumerable<LoginViewModel>>(_loginApp.GetAll());
            var validar = loginViewModel.Where(a => a.Usuario.Equals(usuario.Usuario) && a.Senha.Equals(usuario.Senha)).FirstOrDefault();
            if (validar != null)
            {
                Session["UsuarioId"] = validar.LoginId.ToString();
                Session["Usuario"] = validar.Usuario;
                return RedirectToAction("Logado");
            }
        }
        var errors = ModelState.Values.SelectMany(v => v.Errors);
    }
    catch (Exception ex)
    {

        return RedirectToAction("Index");
    }

    return View(usuario); 
}

Login class:

 public class Login
   {
     public int LoginId { get; set; }
     public string Nome { get; set; }
     public string Usuario { get; set; }
     public string Senha { get; set; }
     public string Email { get; set; }
     public DateTime DataCadastro { get; set; }

    }

View Login:

  @using (Html.BeginForm("Login", "Home", new { ReturnUrl =     ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal" }))
  {
   @Html.AntiForgeryToken();

@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="container">
    <div class="form-login">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="panel-title">Login do Sistema</div>
            </div>

            <div style="padding-top:30px" class="panel-body">
                <div style="display:none" id="result" class="alert alert-danger col-sm-12">

                </div>

                <div style="margin-bottom: 25px" class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    @Html.EditorFor(model => model.Usuario, new { htmlAttributes = new { @class = "form-control input-lg", placeholder = "Usuario", autofocus = true } })
                    @Html.ValidationMessageFor(model => model.Usuario, "", new { @class = "text-danger" })
                </div>

                <div style="margin-bottom: 25px" class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                    @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control input-lg", placeholder = "Senha" } })
                    @Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
                </div>

                <div style="margin-top:10px" class="form-group">

                    <div class="col-sm-12 controls">
                        <input type="submit" value="Acessar" class="btn primary btn-lg" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
  }
  • Ask the Login and View Login class!

  • It has how to place a representation of the object usuario at the time the method is called Login?

  • is there, the class, and the view...

  • What appears in errors?

  • appears System.Linq.Enumerable.Selectmanyiterator<System.Web.Mvc.Modelstate,System.Web.Mvc.Modelerror>

1 answer

1


When using your class in this way without a viewmodel, fields such as Dataregistration are mandatory not optional and Isvalid will return false as they were not received because they do not exist in your form. This way the field is invalid, I believe the correct is not to submit to the Isvalid method, but to test only if the user or password were received via IF. Example: if( usuario. Name != "" && usuario.Password != "" ) { }

Browser other questions tagged

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