如何检查用户是否已经在ASP.NET MVC 5客户端的存在? [英] How to check if user already exists on client-side in ASP.NET MVC 5?
问题描述
使用的 Visual Studio的2013.4 (适用的Visual Studio 2013更新4)我创建了一个普通的 ASP.NET MVC 5 项目,单个用户帐户身份验证配置。所有的用户注册和登录功能已经由Visual Studio中已经脚手架为我和正常工作。
Using Visual Studio 2013.4 (Visual Studio 2013 Update 4) I have created a regular ASP.NET MVC 5 project with Individual User Accounts authentication configuration. All the users registration and log-in features has been already scaffolded for me by Visual Studio and works fine.
如何实现注册页面上的以下规则的客户端验证:有没有已经注册使用相同的电子邮件用户
How to implement client-side validation of the following rule on the registration page: There is no already registered user with the same Email?
推荐答案
您可以使用<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v=vs.118).aspx\">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.
这篇关于如何检查用户是否已经在ASP.NET MVC 5客户端的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!