asp.net-mvc-5 – 电子邮件不能为空或空. MVC 5,Identity 2.0.0

我刚刚将我的代码从Identity 1.0.0完全迁移到2.0.0&由于某种原因,我遇到了一个奇怪的问题.

在尝试创建用户时,它无法成功.

错误是:电子邮件不能为空或空. (当然,我当然会向酒店插入有效的电子邮件).

请帮忙..

Controller.cs

        [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {            
        if (ModelState.IsValid)
        {         
            var user = new ApplicationUser()
            {
                UserName = model.UserName,
                contact_firstname = model.contact_firstname,
                contact_lastname = model.contact_lastname,
                Email = model.Email,
                PhoneNumber = model.PhoneNumber,
                contact_phone2 = model.contact_phone2,
                street = model.street,
                city = model.city,
                country = model.country,
                state = model.state,
                postcode = model.postcode,
                longitude = model.longitude,
                latitude = model.latitude
            };

            var result = await UserManager.CreateAsync(user, model.Password);
            //HERE IS THE ERROR
            if (result.Succeeded)
            {                    
                string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);   
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

AccountViewModel.cs

 public class RegisterViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(8, ErrorMessage = "Must be between {2} and {1} characters long.", MinimumLength = 4)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "First Name")]
    public string contact_firstname { set; get; }

    [Required]
    [Display(Name = "Last Name")]
    public string contact_lastname { set; get; }

    [Required]
    [Phone]
    [Display(Name = "Phone Number")]
    public string PhoneNumber { set; get; }

    [Phone]
    [Display(Name = "Additonal Phone")]
    public string contact_phone2 { set; get; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Country")]
    public string country { set; get; }

    [Display(Name = "State")]
    public string state { set; get; }

    [Required]
    [Display(Name = "City")]
    public string city { set; get; }

    [Required]
    [Display(Name = "Home Address")]
    public string street { set; get; }

    [Required]
    [Display(Name = "Postal / Zip Code")]
    public string postcode { set; get; }

    public double longitude { set; get; }

    public double latitude { set; get; }               
}

最佳答案 从您的AccountViewModel.cs,将您的[EmailAddress]更改为[DataType(DataType.EmailAddress,ErrorMessage =“电子邮件无效”)]

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注