在 mvc 中创建时与下拉列表中的对象进行交互

发布于 2024-12-10 17:23:57 字数 2142 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-12-17 17:23:57

您可以通过将 RoleId 外键添加为 User 的属性来公开它。

那么你的观点会是这样的

<div class="editor-field">
    @Html.DropDownListFor(model => model.RoleId, (SelectList)ViewBag.Role)
    @Html.ValidationMessageFor(model => model.RoleId)
</div> 

You can expose the RoleId foreign key by adding it as a property of User.

Then your view would look like

<div class="editor-field">
    @Html.DropDownListFor(model => model.RoleId, (SelectList)ViewBag.Role)
    @Html.ValidationMessageFor(model => model.RoleId)
</div> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文