身份 - 与RegisterViewModel的一对一关系



我是MVC的新手,并且一直在用这头拔出头发。我已经大胆了,所以需要有人营救剩下的东西!

我正在使用asp.net Identity,并尝试在RegisterViewModel和Storedetails之间创建一对一关系,真的希望有人可以帮助我!

这是我的RegisterViewModel

   public class RegisterViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { 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")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
    public virtual StoreDetails StoreDetails { get; set; }
}

这是我的存储模型:

public class StoreDetails
{
    [Key, ForeignKey("RegisterViewModel")]
    public string StoreName { get; set; }
    public virtual RegisterViewModel RegisterViewModel { get; set; }
}

控制器:

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser {
                // PERSONAL DETAILS
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,
                LastName = model.LastName
            };
            var storeDetails = new StoreDetails {
                // STORE DETAILS
                StoreName = model.StoreDetails.StoreName
            };

            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 https://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // 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>");

                // UPDATE DB WITH STORE DETAILS DATA
                var db = new ApplicationDbContext();
                db.StoreDetails.Add(new StoreDetails
                {
                    StoreName = model.StoreDetails.StoreName
                });
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

我要实现的目标是在注册上使用以下输入生成储存表:

@Html.TextBoxFor(m => m.StoreDetails.StoreName)

如果您需要任何其他详细信息,请询问

谢谢

registerviewModel不是一个真实的实体,即通过EF保存到数据库中的人。它只是将电子邮件和密码值传递到称为ApplicationUser的真实用户/人员对象的载体对象。这是一个视图模型,而不是真实的模型。

在您的示例中,您在registerviewModel中添加了储藏室,这是正确的。但是您还需要将其添加到应用程序类属性中。

public class ApplicationUser {
public virtual StoreDetails StoreDetails { get; set; }

然后运行迁移。

将任何内容添加到registerviewModel是从视图层传递数据的,以安全守护它进入您的DB的对象。

即这样做什么都没有做任何事情,因为ViewModel不住在DB

public class StoreDetails
{
    [Key, ForeignKey("RegisterViewModel")] //this will not work remove
    public string StoreName { get; set; }
    public virtual RegisterViewModel RegisterViewModel { get; set; }// remove
}
public Ienumerable<StoreDetails> StoreDetails { get; set; }
public int StoreDetailsId { get; set; }

在控制器中:

using (var db = ApplicationDbContext.Create())
{
    model.StoreDetails = db.StoreDetails.ToList();
}

相关内容

  • 没有找到相关文章