Entity framework 数据批注检查唯一字段
我试图检查用户名是否已经存在于注册系统的数据库中。但是在实现了这个功能之后,我的注册系统就不工作了,他也没有告诉我用户名是否存在 型号:Entity framework 数据批注检查唯一字段,entity-framework,asp.net-mvc-4,razor,data-annotations,Entity Framework,Asp.net Mvc 4,Razor,Data Annotations,我试图检查用户名是否已经存在于注册系统的数据库中。但是在实现了这个功能之后,我的注册系统就不工作了,他也没有告诉我用户名是否存在 型号: public class RegisterModel { [Required] [Remote("IsUserExist", "Account", ErrorMessage = "User already exist!")] [Display(Name = "Nom d'utilisateur")] public str
public class RegisterModel
{
[Required]
[Remote("IsUserExist", "Account", ErrorMessage = "User already exist!")]
[Display(Name = "Nom d'utilisateur")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "La chaîne {0} doit comporter au moins {2} caractères.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Mot de passe")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirmer le mot de passe ")]
[Compare("Password", ErrorMessage = "Le mot de passe et le mot de passe de confirmation ne correspondent pas.")]
public string ConfirmPassword { get; set; }
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Prénom")]
public string Fname { get; set; }
[Required]
[Display(Name = "Nom")]
public string Lname { get; set; }
}
iUserExist
public JsonResult IsUserExist(string username)
{
var exist = WebSecurity.UserExists(username);
return Json(!exist, JsonRequestBehavior.AllowGet);
}
我的看法是:
@model Projet2.Models.RegisterModel
@{
ViewBag.Title = "S'inscrire";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Créez un nouveau compte.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Formulaire d'inscription</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.Lname)
@Html.TextBoxFor(m => m.Lname)
</li>
<li>
@Html.LabelFor(m => m.Fname)
@Html.TextBoxFor(m => m.Fname)
</li>
</ol>
<input type="submit" value="S'inscrire" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/Scripts/jquery-1.8.2.min.js")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
@Scripts.Render("~/Scripts/buttonset.js")
@Scripts.Render("~/Scripts/input-control.js")
}
@model Projet2.Models.RegisterModel
@{
ViewBag.Title=“S'inscrire”;
}
@ViewBag.Title。
这是一家新公司。
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary()
公式铭文
@LabelFor(m=>m.UserName)
@Html.TextBoxFor(m=>m.UserName)
@LabelFor(m=>m.Password)
@Html.PasswordFor(m=>m.Password)
@LabelFor(m=>m.ConfirmPassword)
@Html.PasswordFor(m=>m.ConfirmPassword)
@LabelFor(m=>m.Email)
@Html.TextBoxFor(m=>m.Email)
@LabelFor(m=>m.Lname)
@Html.TextBoxFor(m=>m.Lname)
@LabelFor(m=>m.Fname)
@Html.TextBoxFor(m=>m.Fname)
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
@Scripts.Render(“~/bundles/modernizer”)
@Scripts.Render(“~/Scripts/jquery-1.8.2.min.js”)
@Scripts.Render(“~/Scripts/jquery.validate.js”)
@Scripts.Render(“~/Scripts/jquery.validate.unobtrusive.js”)
@Scripts.Render(“~/Scripts/buttonset.js”)
@Scripts.Render(“~/Scripts/input control.js”)
}
是我的代码中有错误还是我必须做其他事情
提前谢谢
Pokerstarr那么您正在点击IsUserExist和WebSecurity。UserExists(username)返回预期值?您在web.config中有正确的应用程序设置吗?也可能[OutputCache(Location=OutputCacheLocation.None,NoStore=true)]appSettings位于web.config中,但数据注释似乎没有调用IsUserExist。我从你的URL中获取代码并对其进行修改,但这与他不调用IsUserExit是一样的。那么IsUserExist()在帐户控制器中?您可能需要添加[AllowAnonymous]或查看其他安全问题/过滤器。我的错,您是对的,这是安全过滤器的问题。谢谢你的帮助!:)