在 mvc 中创建时与下拉列表中的对象进行交互
我有 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; }
}
我创建了带有读/写实体的 UserController,在 Create 方法中,我创建了 ViewBag.Role 包含角色列表
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>
在带有 HttpPost 属性的创建视图中,当用户选择一个角色,然后提交,我无法获得角色的值。 有人知道如何通过选择下拉列表将角色添加到用户中吗?我的意思是,当我下拉列表角色并选择其中的项目并提交时,我将一个用户行添加到表中 谢谢
I have 2 classes: User and Role, an user has a role
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; }
}
I created UserController with read/write entity, in Create method, I created ViewBag.Role contains list of role
public ActionResult Create()
{
ViewBag.Role = new SelectList(db.Roles, "RoleId", "Name");
return View();
}
In create 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将 RoleId 外键添加为 User 的属性来公开它。
那么你的观点会是这样的
You can expose the RoleId foreign key by adding it as a property of User.
Then your view would look like