要解决ASP.NET MVC 5中远程验证不起作用的问题,可以按照以下步骤进行操作:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.LabelFor(model => model.Email, new { @class = "control-label" })
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
@Html.LabelFor(model => model.Username, new { @class = "control-label" })
@Html.TextBoxFor(model => model.Username, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Username)
@Html.LabelFor(model => model.Password, new { @class = "control-label" })
@Html.PasswordFor(model => model.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Password)
@Html.LabelFor(model => model.ConfirmPassword, new { @class = "control-label" })
@Html.PasswordFor(model => model.ConfirmPassword, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ConfirmPassword)
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task IsUsernameAvailable(string username)
{
var user = await UserManager.FindByNameAsync(username);
bool isAvailable = (user == null);
return Json(isAvailable, JsonRequestBehavior.AllowGet);
}
使用上述方法,可以解决ASP.NET MVC 5中远程验证不起作用的问题。