Error while logging in

0

I created a View here to login on my system, but in the ModelState.IsValid part only returns false , I do not know the reason, could someone help?

My code for 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>
  }
    
asked by anonymous 14.06.2016 / 02:31

1 answer

1

When using your class in this way without a viewmodel, fields like DataCadastro are mandatory not optional and Isvalid will return false because they were not received because they do not exist on your form. In this way the field is invalid, I believe the correct one is do not submit to the IsValid method and yes, test only if the user or password were received via IF. Example: if (user.name!="" & & user.sample!="") { }

    
23.06.2016 / 15:57