2014-05-24 92 views
-1

嗨,我有点新MVC和实体框架,所以我想不出如何解决这个问题。我搜查了(相信我)一个awnser,但没有结果。 我正尝试使用实体框架登录我的项目。事情是我补充说:ModelState.IsValid = false MVC 4实体框架

public string Confirmar_contraseña { get; set; }

到“用户”的模式,以验证(当你在注册屏幕)的密码是否相等

型号:

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; } 
    } 
} 

查看:

@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") 
} 

控制器

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(); 
     } 

    } 
} 

西班牙语中有一些单词,对不起。重点是我使用“Confirmar_contraseña”作为数据库实体模型的辅助属性,只是为了确认用户输入相同的密码,但是当我尝试登录时,ModelState返回false,我不知道为什么。

+0

显示你的模型,其中的ModelState声称是无效的actionmethod。 – CodeCaster

+0

更多代码请!你只问了半个问题。 '你添加了x来验证密码,但是.....' –

+0

Im对此,使用这个编辑器真的很难--- – N3HL

回答

0

要检查的ModelState错误是什么,加在后操作方法如下,并检查errors财产

if (!ModelState.IsValid) 
{ 
    var errors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors); 
    .... 
+0

[Errors var](http://puu.sh/8YUPB.png ) 这是从debuggin – N3HL

+0

我得到什么你需要展开列表,看看他们是什么 –

+0

[错误2](http://puu.sh/8YV78.png) 它说:“密码不匹配” ,就像我在mo中使用的DataAnnotation一样del,但我正在尝试登录,而不是注册我不明白 – N3HL