我在ASP.NET中使用远程验证时遇到了一些问题: 一切正常,但我的方法是错误地考虑登录页面进行验证,而不是注册页面。
public JsonResult IsUserAvailable(string UserName) {
using (LeafDbContext db = new LeafDbContext()) {
return Json(!db.userAccount.Any(User => User.UserName == UserName),JsonRequestBehavior.AllowGet);
}
}
注意:AccountsController它与我用来处理所有注册内容的同一个控制器(密码哈希,发送确认邮件等)。
public class LoginUserAccount {
//...bunch of other fields required for registration
[Required(ErrorMessage = "Um nome de usuário é obrigatório.")]
[Remote("IsUserAvailable","Accounts",ErrorMessage = "O usuário já existe.")]
public string UserName { get; set; }
}
注意:ErrorMessage
用葡萄牙语,请忽略它。
@model LEAF.Models.LoginUserAccount
@{
ViewBag.Title = "Register";
}
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<!-- bunch of other fields required for registration-->
<!-- and my UserName input right below -->
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
注意:我使用的是与Login.cshtml相同的代码(包括@model LEAF.Models.LoginUserAccount
),但是登录验证是由另一个控制器完成的。
嗯,这确实有效,但不正确。当我运行应用程序并重定向到我的登录页面时,在我的UserName输入上进行远程验证,而不是在我的注册页面中。如何将其更改为我的注册页面?