MVC5自定义身份用户



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

DepotID和DepartmentID 的RegisterViewModel Register方法错误

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; }
}

部门.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; }
}

Depot.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; }

}

您正试图为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的情况与相同

相关内容

  • 没有找到相关文章

最新更新