我有2个班级: 用户和作用
public class User : IPrincipal
{
public int UserId { get; set; }
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Created date")]
public DateTime CreatedDate { get; set; }
[Required]
[Display(Name = "Password question")]
public string PasswordQuestion { get; set; }
[Required]
[Display(Name = "Password anwser")]
public string PasswordAnswer { get; set; }
[Display(Name = "Active")]
public bool IsApproved { get; set; }
[Display(Name = "Last login date")]
public DateTime LastLoginDate { get; set; }
[Display(Name = "Last password changed date")]
public DateTime LastPasswordChangedDate { get; set; }
[Display(Name = "Last activity date")]
public DateTime LastActivityDate { get; set; }
public virtual Role Role { get; set; }
public virtual IIdentity Identity { get; set; }
public virtual bool IsInRole(string role)
{
if (Role.Name.ToLower() == role.ToLower())
{
return true;
}
return false;
}
}
public class Role
{
public int RoleId { get; set; }
[Required]
[Display(Name = "Role name")]
public string Name { get; set; }
}
我设立了阅读/标准实体的用户主计长,在制作方法时,我设立了“观点”。 作用清单
public ActionResult Create()
{
ViewBag.Role = new SelectList(db.Roles, "RoleId", "Name");
return View();
}
目的是:
<div class="editor-label">
@Html.LabelFor(model => model.Role)
</div>
<div class="editor-field">
@Html.DropDownList("Role")
@Html.ValidationMessageFor(model => model.Role.RoleId) @* I don t know this is correct? *@
</div>
In create view with HttpPost attribute, when user choose one role, and submit, I can t get a value of role. Anyone who know the way to add role into user by select dropdownlist? I means, when I drop down list role and select item in there, and submit, I add one user row into table Thank you