0

I have changed the RegisterViewModel to use Username instead of Email and omitted the email part when initialized new ApplicationUser in the Register POST method:

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

But when I register by username I get the error:

Email cannot be null or empty.

How can I solve this, and is there a way to not allow identical usernames?

mshwf
  • 7,009
  • 12
  • 59
  • 133
  • Check your code for any other places that the email variable is used and change them accordingly. – Captain Squirrel Nov 09 '16 at 12:40
  • Possible duplicate of [Use Username instead of Email for identity in Asp.net mvc5](http://stackoverflow.com/questions/27501533/use-username-instead-of-email-for-identity-in-asp-net-mvc5) – Mladen Oršolić Nov 09 '16 at 12:41

1 Answers1

0

you can change your RegisterViewModel to

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

}

and write your exist validation actionresul in your 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);
    }
Salar Afshar
  • 229
  • 1
  • 2
  • 13