使用asp.net Identity捕获用户注册的当前DateTime

时间:2017-12-01 12:02:29

标签: c# asp.net datetime asp.net-identity

我试图捕获使用我的asp.net身份registerViewModel注册用户的日期和时间。然后我想将它添加到AspNetUsers表中。

MODEL:

foldr (\x ante->[x]:ante) [[4]] [1,2,3]

控制器:

public class RegisterViewModel
    {
        public DateTime DateRegistered { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} 
        characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

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

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

我一直收到错误:

  

'无法从RegisterViewModel转换为ApplicationUser。'   ' RegisterViewModel不能分配给参数类型   ApplicationUser'

此错误在以下代码的(模型)位上:

// GET: /Account/Register
    [HttpGet]
    [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }


    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser
            {
                UserName = model.UserName
        };

            var db = new ApplicationDbContext();
            model.GetDate = DateTime.Now;
            db.Users.Add(model);
            db.SaveChanges();

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                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>");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

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

对此的任何帮助都会很棒。感谢

1 个答案:

答案 0 :(得分:2)

db.Users.Add(model);

Users表格为Set<ApplicationUser>。它只允许您向其添加ApplicationUser个对象。

Register(RegisterViewModel model)

modelRegisterViewModel类型的参数。这不是ApplicationUser类型的参数,因此无法添加到Users表中。

我认为你想要的是这个:

db.Users.Add(user);

由于user的类型为ApplicationUser

作为一般提示,当您看到以下错误消息时:

  

无法从 RegisterViewModel 转换为 ApplicationUser

你应该把它读作:

  

当您使用 ApplicationUser 对象时,您正尝试使用 RegisterViewModel 对象。这不计算。使用正确类型的对象。