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?

2

1 Answer 1

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.