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!
– user46523
It has how to place a representation of the object
usuario
at the time the method is calledLogin
?– user48732
is there, the class, and the view...
– Desalex
What appears in
errors
?– Leonel Sanches da Silva
appears System.Linq.Enumerable.Selectmanyiterator<System.Web.Mvc.Modelstate,System.Web.Mvc.Modelerror>
– Desalex