【问题标题】:MVC5 Customising Identity UserMVC5 自定义身份用户
【发布时间】:2016-03-27 01:34:47
【问题描述】:

我正在尝试将一些属性添加到我的 registerviewmodel 中,以便当用户在我的网站上注册时,他们需要添加他们的 LastName、FirstMidName、DepotID 和 DepartmentID。当我尝试修改我的 RegisterViewModel 创建方法时出现错误。

RegisterViewModel DepotID 和 DepartmentID 的注册方法错误

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

            user.LastName = model.LastName;
            user.FirstMidName = model.FirstMidName;
            user.Depot = model.DepotID;
            //Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Depot
            user.Department = model.DepartmentID;
            //Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Department
            user.EnrollmentDate = model.EnrollmentDate;

IdentityModel.cs(自定义用户身份)

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> 
        GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager
            .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public bool IsAdministrator { get; set; }
    [StringLength(50, MinimumLength = 1)]


    public string Address { get; set; }

    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    public string FullName
    {
        get { return FirstMidName + " " + LastName; }
    }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }
    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }
    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
}


public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }
    public ApplicationRole(string name) : base(name) { }
    public string Description { get; set; }

}

AccountController.cs中的RegisterViewModel注册方法

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

            // Add the Address properties:
            user.LastName = model.LastName;
            user.FirstMidName = model.FirstMidName;
            user.Depot = model.DepotID;
            user.Department = model.DepartmentID;
            user.EnrollmentDate = model.EnrollmentDate;

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var 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 this link: <a href=\"" 
                    + callbackUrl + "\">link</a>");
                ViewBag.Link = callbackUrl;
                return View("DisplayEmail");
            }
            AddErrors(result);
        }

        return View(model);
    }

AccountsViewModels.cs 中的RegisterViewModel 类

public class RegisterViewModel
{
    [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 string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    public string FullName
    {
        get { return FirstMidName + " " + LastName; }
    }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public int DepartmentID { get; set; }
    public int DepotID { get; set; }

}

Department.cs

public class Department
{

    public int DepartmentID { get; set; }

    [StringLength(50, MinimumLength = 1)]
    public string DepartmentName { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

仓库.cs

public class Depot
{

    public int DepotID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string DepotName { get; set; }
    public virtual ICollection<User> Users { get; set; }

}

MVC4 中的旧用户类

public class User
{
    public int UserID { get; set; }

    public bool IsAdministrator { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    public string FullName
    {
        get { return FirstMidName +" "+ LastName; }
    }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }
    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }


}

【问题讨论】:

    标签: c# asp.net-mvc entity-framework asp.net-identity


    【解决方案1】:

    您正在尝试将整数值分配给 model

    //Cannot implicitly convert type 'int' to RecreationalServicesTickingSystem.Models.Depot
                    user.Department = model.DepartmentID;
    //Cannot implicitly convert type 'int' to 'RecreationalServicesTickingSystem.Models.Department
                    user.EnrollmentDate = model.EnrollmentDate;
    

    您的模型ApplicationUser

     public int DepartmentID { get; set; }
    
     [ForeignKey("DepartmentID")]
     public virtual Department Department { get; set; }
    

    正确的代码应该是:

    //To assign to DepartmentID
      user.DepartmentID = model.DepartmentID;
    //OR
    //to assign to model's departmentID
      user.Department.DepartmentID = model.DepartmentID;
    

    Depot 也一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多