TykiMikk
TykiMikk

Reputation: 1068

MVC5 Customising Identity User

I'm trying to add a few properties into my registerviewmodel so that when a user registers on my site they'll need to add their LastName,FirstMidName,DepotID and DepartmentID. I'm getting the errors when I try to modify my RegisterViewModel Create Method.

RegisterViewModel Register method error for DepotID and 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 (Customised UserIdentity)

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

}

RegisterViewModel Register Method in AccountController.cs

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

RegisterViewModel Class in AccountsViewModels.cs

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

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

}

Old User Class in 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; }


}

Upvotes: 1

Views: 179

Answers (1)

Polynomial Proton
Polynomial Proton

Reputation: 5135

You are trying to assign an integer value to a 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;

Your model ApplicationUser has

 public int DepartmentID { get; set; }

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

Correct code should be:

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

Same case for Depot too

Upvotes: 2

Related Questions