我尝试在registerviewmodel中添加一些属性,以便当用户在我的网站上注册时,他们需要添加他们的LastName,FirstMidName,DepotID和DepartmentID。我在尝试修改RegisterViewModel创建方法时遇到错误。
RegisterViewModel注册DepotID和DepartmentID的方法错误
2
IdentityModel.cs(Customized UserIdentity)
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;
AccountController.cs中的RegisterViewModel注册方法
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; }
}
AccountsViewModels.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);
}
Department.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; }
}
Depot.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; }
}
MVC4中的旧用户类
public class Depot
{
public int DepotID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string DepotName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
答案 0 :(得分:2)
您正尝试将整数值分配给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
的情况也是如此