我将RegisterViewModel
改为使用Username
而不是Email
,并且在Register
POST方法中初始化new ApplicationUser
时省略了email部分:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Username/*, Email = model.Username*/ };
...
但是当我用用户名注册时,我得到错误:
Email不能为空
我该如何解决这个问题,是否有一种方法不允许相同的用户名?
您可以将RegisterViewModel更改为
public class RegisterViewModel
{
[Remote("IsUserNameExist", "User", "", ErrorMessage = "this username exist", HttpMethod = "POST")]
public string UserName { set; get; }
//todo: other fileds......
}
并在UserController
中写入您的存在验证操作结果 [HttpPost]
[AllowAnonymous]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true, Duration = 0, VaryByParam = "*")]
public virtual JsonResult IsUserNameExist(string userName)
{
var check = _userService.CheckUserNameExist(userName);
return check ? Json(false) : Json(true);
}