简体   繁体   English

MVC3角色管理复选框列表

[英]MVC3 List of Checkboxes for Role Management

I am trying to manage roles in an MVC3 app. 我正在尝试管理MVC3应用程序中的角色。 The idea is that I have a list of users and when I click on the Edit Roles button on the row I get a modal window with a list of all possible roles with the ones the user is a member of checked. 我的想法是我有一个用户列表,当我点击行上的编辑角色按钮时,我得到一个模态窗口,其中包含所有可能角色的列表,其中用户是已检查的成员。

Then I can select the new roles and click save and send an ajax post back to the server to persist the changes. 然后我可以选择新角色并单击“保存”并将ajax发送回服务器以保留更改。

I have the modal popping up, but I'm not sure how to generate the checkboxes in a way that its easy to submit back to the server upon changing. 我有模式弹出,但我不知道如何以一种易于在更改时提交回服务器的方式生成复选框。 I want the simplest solution possible. 我想要最简单的解决方案。

Here is what I have for the partial view that the modal is populated with when you click Edit Roles: 以下是我点击编辑角色时填充模态的局部视图:

public ActionResult ChooseRolePartial(string username)
    {
        var userRoles = Roles.GetRolesForUser(username);
        var list = new MultiSelectList(Roles.GetAllRoles());
        foreach (var item in list)
        {
            item.Selected = userRoles.Contains(item.Value);
        }

        var model = new ChooseRoleModel
        {
             Roles = list,
             Username = username
        };

        return PartialView("Partials/ChooseRolePartial", model);
    }

I was hoping there was an EditorFor for MultiSelectList and it would all be handled for me. 我希望MultiSelectList有一个EditorFor,它将全部为我处理。 But that doesn't appear to be the case. 但事实似乎并非如此。 It just renders the text false for each of my roles. 它只是为我的每个角色呈现文本false。

Whats the best way to generate this list of checkboxs and submit whats checked along with the username back to the server? 什么是生成此复选框列表的最佳方法,并将用户名和选中的内容提交回服务器?

Model: 模型:

public class ChooseRoleModel
{
    public SelectListItem[] Roles { get; set; }
    public string Username { get; set; }
}

Controller: 控制器:

public class RolesController : Controller
{
    ...

    public ActionResult ChooseRolePartial(string username)
    {
        var userRoles = Roles.GetRolesForUser(username);
        var roles = Roles.GetAllRoles().Select(x => new SelectListItem
        {
            Value = x,
            Text = x,
            Selected = userRoles.Contains(x)
        }).ToArray();

        var model = new ChooseRoleModel
        {
            Roles = roles,
            Username = username
        };

        return PartialView("Partials/ChooseRolePartial", model);
    }

    [HttpPost]
    public ActionResult ChooseRolePartial(ChooseRoleModel model)
    {
        ...
    }
}

View: 视图:

@model ChooseRoleModel

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Username)
        @Html.EditorFor(x => x.Username)
    </div>
    for (int i = 0; i < Model.Roles.Length; i++)
    {
        @Html.CheckBoxFor(x => x.Roles[i].Selected)    
        @Html.LabelFor(x => x.Roles[i].Selected, Model.Roles[i].Text)
        @Html.HiddenFor(x => x.Roles[i].Text)
    }
    <button type="submit">OK</button>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM