简体   繁体   English

MVC 5身份。 用户角色

[英]MVC 5 Identity. Roles for User

I need to display set of checkboxes to being able to manage user roles. 我需要显示一组复选框才能管理用户角色。 I can give to my view all of existing roles: 我可以提出所有现有角色的观点:

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new MlmDbContext()));
ViewBag.allRoles = roleManager.Roles.ToList();

And I can get roles for user into the view : 而且我可以为用户提供角色到视图中:

@{
    foreach (var role in Model.Roles)
 {

But what should I do to show ALL roles with binding to Model.Roles and than save changes? 但是,我应该怎么做才能显示绑定到Model.Roles的所有角色,然后保存更改?

you put the id of the roles as value on the form and then receive a table of int of selected roles il controller 您将角色的ID作为值放在表单上,​​然后接收到一个选定角色的int表il控制器

In one of my projectt this is part of codes that should interest you: 在我的一个项目中,这是您应该感兴趣的代码的一部分:

Controller that provide data to the view: 向视图提供数据的控制器:

public async Task<ActionResult> Create()
        {
            // Get the list of Roles
            ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");

            return View();
        }

View that shows roles: 显示角色的视图:

<div class="form-group">
        <label class="col-md-2 control-label">
            @Resources.Global.SelectRole
        </label>
        <div class="col-md-10">
            @foreach (var item in (SelectList)ViewBag.RoleId)
            {
                <input type="checkbox" name="SelectedRoles" value="@item.Value" class="checkbox-inline" />
                @Html.Label(item.Value, new { @class = "control-label" })
            }
        </div>
    </div>

Controller that receive roles selected : 接收选择角色的控制器:

[HttpPost]
        public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {...}
        }

EDIT: 编辑:

OK so this is what I think you need: 好的,这就是我认为您需要的:

In the Controller: 在控制器中:

RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
            {
                Selected = userRoles.Contains(x.Name),
                Text = x.Name,
                Value = x.Name
            }),

In the view: 在视图中:

<div class="form-group">
            @Html.Label("Roles", new { @class = "control-label col-md-2" })
            <span class=" col-md-10">
                @foreach (var item in Model.RolesList)
                {
                    <input type="checkbox" name="SelectedRole" value="@item.Value" checked="@item.Selected" class="checkbox-inline" />
                    @Html.Label(item.Value, new { @class = "control-label" })
                }
            </span>
        </div>

Here's how I did it. 这是我的方法。

Create an extension method on role manager, replace ApplicationRole with whatever class name you're using for your roles. 在角色管理器上创建扩展方法,将ApplicationRole替换为您用于角色的任何类名。

public static class RoleManagerExtensions
{
    public static List<CheckBoxItem> GetRolesSelectList(
        this RoleManager<ApplicationRole> roleManager,
        IList<string> selectedValues)
    {
        List<CheckBoxItem> roles = 
            roleManager.Roles.ToList().Select(r => new CheckBoxItem
        {
            Selected = selectedValues.Contains(r.Name),
            Id = r.Name,
            Name = r.Name
        }).ToList();
        return roles;
    }
}

Here's the CheckBoxItem POCO: 这是CheckBoxItem POCO:

public class CheckBoxItem
{
    public bool Selected { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
}

In your controller get the roles list and pass in the selected roles: 在控制器中,获取角色列表并传递所选角色:

var selectedRoles = await _userManager.GetRolesAsync(user);
List<CheckBoxItem> roles = _roleManager.GetRolesSelectList(selectedRoles);

In your View, you can create your checkboxes like so: 在您的视图中,您可以如下创建复选框:

 @for (int i = 0; i < Model.Roles.Count; i++)
 {
     <input type="checkbox" asp-for="@Model.Roles[i].Selected" />
     <label asp-for="@Model.Roles[i].Selected">@Model.Roles[i].Name</label>
     <input type="hidden" asp-for="@Model.Roles[i].Id" />
     <input type="hidden" asp-for="@Model.Roles[i].Name" />
 }

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

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