模型状态.MVC 4实体框架
本文关键字:实体 框架 MVC 状态 模型 | 更新日期: 2023-09-27 18:04:48
嗨,我有点新的MVC和实体框架,所以我不知道如何解决这个问题。我(相信我)搜索了一个答案,但没有结果。我试图登录我的项目使用实体框架。问题是我加了:
public string Confirmar_contraseña { get; set; }
到"Users"模型来验证密码是否相等(当您在注册屏幕时)
模型:
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,我不知道为什么。
要检查ModelState错误是什么,请在post action方法中添加以下内容并检查errors
属性
if (!ModelState.IsValid)
{
var errors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
....