我目前正在在用戶輸入他們的電子郵件地址的註冊頁面。 我希望每封電子郵件都是唯一的。MVC遠程驗證(唯一的電子郵件地址)
這是我RegisterModel的一部分:
[Required()]
[System.Web.Mvc.Remote("IsUserEmailAvailable", "Account")]
[EmailAddress()]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string Email { get; set; }
..
這是我的AccountController的一部分:
public JsonResult IsUserEmailAvailable(string UserEmail)
{
return Json(!db.UserProfiles.Any(User => User.UserName == UserEmail), JsonRequestBehavior.AllowGet);
}
這是我的我的看法:
<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<hgroup class="title">
<h1>Create Account</h1>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</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.ConfirmEmail)
@Html.TextBoxFor(m => m.ConfirmEmail)
</li>
</ol>
<input type="submit" value="Registrera" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
如果我刪除[System.Web.Mvc.Remote(「IsUserEmailAvailable」,「Account」)]一切都很好,除了電子郵件地址不會是唯一的。有了它,當我按提交時,沒有任何反應。
我錯過了什麼?
你能展示你的觀點嗎?如可能你錯過了展示驗證錯誤Html.ValidationMessageFor –
編輯我的帖子:) – Reft
嘗試增加html.ValidationMesageFor電子郵件 –