且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查用户是否已经在ASP.NET MVC 5客户端的存在?

更新时间: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.