更新时间:2023-11-26 14:31:52
您可以使用RemoteAttribute与服务器回调执行客户端验证。
You could use RemoteAttribute to perform client side validation with a server callback.
1)添加以下方法到的AccountController
:
1) Add the following method to the AccountController
:
[AllowAnonymous]
public async Task<JsonResult> UserAlreadyExistsAsync(string email)
{
var result =
await userManager.FindByNameAsync(email) ??
await userManager.FindByEmailAsync(email);
return Json(result == null, JsonRequestBehavior.AllowGet);
}
2)添加远程
属性电子邮件
的属性 RegisterViewModel
类:
[Remote("UserAlreadyExistsAsync", "Account", ErrorMessage = "User with this Email already exists")]
public string Email { get; set; }
其中,帐户
是服务控制器的名称,UserAlreadyExistsAsync
是它的动作名称。
where "Account"
is the name of the serving controller and "UserAlreadyExistsAsync"
is it's action name.