2016-11-09 4 views
0

私はUsernameの代わりEmailを使用するRegisterViewModelを変更し、Register POSTメソッドでnew ApplicationUserを初期化したときに電子メールの部分を省略していますASP.NET MVC 5で電子メールの代わりにユーザー名を使用して登録しますか?

 public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser { UserName = model.Username/*, Email = model.Username*/ }; 
... 

しかし、私は、ユーザー名で登録すると、私はエラーを取得する:

Email cannot be null or empty.

これを解決するにはどうすればよいのですか?同一のユーザー名を許可しない方法はありますか?

+0

をあなたが存在検証actionresulを書くことができ、電子メールの変数を使用して、適切に変更され、他の場所のためのあなたのコードを確認してください。 –

+0

「[Asp.net mvc5のアイデンティティのために電子メールの代わりにユーザー名を使用する]」の可能な複製(http://stackoverflow.com/questions/27501533/use-username-instead-of-email-for-identity-in-asp-net) -mvc5) –

答えて

0

あなたは

public class RegisterViewModel 
{ 
    [Remote("IsUserNameExist", "User", "", ErrorMessage = "this username exist", HttpMethod = "POST")] 
    public string UserName { set; get; } 
    //todo: other fileds...... 

} 

にごRegisterViewModelを変更して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); 
    } 
関連する問題