Reputation: 87
Hi, I'm kinda new to MVC and Entity Framework, so I can't figure out how to resolve this problem. I searched (trust me) for an awnser but got no results. I'm trying to login on my project using Entity Framework. The thing is I added:
public string Confirmar_contraseña { get; set; }
to "Users" model to verify if passwords are equal or not (when you are in the register screen)
Model:
namespace MundialDeFutbol.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
public partial class Usuarios
{
public int Id { get; set; }
[Required(ErrorMessage = "El nick (nombre de usuario) es obligatoio")]
[StringLength(15, MinimumLength = 3, ErrorMessage = "El nombre de usuario debe contener entre 3 y 15 caracteres")]
public string Nick { get; set; }
[Required(ErrorMessage = "La contraseña es obligatoria")]
[StringLength(10, MinimumLength = 4, ErrorMessage = "La contraseña debe contener entre 4 y 10 caracteres")]
public string Contraseña { get; set; }
[NotMapped]
[Compare("Contraseña", ErrorMessage = "Las contraseñas no coinciden")]
public string Confirmar_contraseña { get; set; }
public int Tipo_de_usuario { get; set; }
}
}
View:
@using MundialDeFutbol.Models
@using System.Web.Optimization
@model Usuarios
@{
ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm("Login", "Usuarios", FormMethod.Post, new { @class = "form-horizontal", @style = "margin-top: 20px;" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form-group">
<label for="Nick" class="col-sm-2 control-label">@Html.LabelFor(model => model.Nick)</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Nick, new { @class = "form-control", @placeholder = "Nombre de usuario" })
<strong>
@Html.ValidationMessageFor(model => model.Nick, null, new { @class = "text-danger", @style = "display: block; margin-top: 10px;" })
</strong>
</div>
</div>
<div class="form-group">
<label for="Contraseña" class="col-sm-2 control-label">@Html.LabelFor(model => model.Contraseña)</label>
<div class="col-sm-10">
@Html.TextBoxFor(model => model.Contraseña, new { @class = "form-control", @placeholder = "Escribe contraseña", @type = "password" })
<strong>
@Html.ValidationMessageFor(model => model.Contraseña, null, new { @class = "text-danger", @style = "display: block; margin-top: 10px;" })
</strong>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Enviar</button>
</div>
</div>
}
@section scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Controller
namespace MundialDeFutbol.Controllers
{
public class UsuariosController : Controller
{
//
// GET: /Usuarios/
public ActionResult Index()
{
return View();
}
public ActionResult Registrar()
{
return View();
}
[HttpPost]
public ActionResult Registrar(Usuarios usuario)
{
if (ModelState.IsValid)
{
var ctx = new MundialDBEntities();
ctx.Usuarios.Add(usuario);
ctx.SaveChanges();
return RedirectToAction("Index","Home");
}
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Usuarios usuario)
{
if (ModelState.IsValid)
{
FormsAuthentication.SetAuthCookie(usuario.Nick, false);
//This needs to be completed I think
return RedirectToAction("Index", "Home");
}
return View();
}
}
}
There are some words in spanish, sorry for that. The point is I used "Confirmar_contraseña" as a auxiliar attribute on database entity model, just to confirm the user type the same password, but when I try to login, ModelState returns false, I don't know why.
Upvotes: 2
Views: 3997
Reputation:
To check what the ModelState errors are, add the following in the post action method and examine the errors
property
if (!ModelState.IsValid)
{
var errors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
....
Upvotes: 4