MVC 5 身份。用户角色
MVC 5 Identity. Roles for User
我需要显示一组复选框才能管理用户角色。
我可以给出所有现有角色的观点:
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new MlmDbContext()));
ViewBag.allRoles = roleManager.Roles.ToList();
我可以将用户的角色添加到视图中:
@{
foreach (var role in Model.Roles)
{
但是我应该怎么做才能显示绑定到 Model.Roles 的所有角色而不是保存更改?
你将角色的 id 作为值放在表单上,然后收到 table 所选角色 il controller
在我的一个项目中,这是您应该感兴趣的代码的一部分:
向视图提供数据的控制器:
public async Task<ActionResult> Create()
{
// Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
return View();
}
显示角色的视图:
<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>
接收所选角色的控制器:
[HttpPost]
public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
{
if (ModelState.IsValid)
{...}
}
编辑:
好的,这就是我认为您需要的:
在控制器中:
RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
{
Selected = userRoles.Contains(x.Name),
Text = x.Name,
Value = x.Name
}),
在视图中:
<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>
我是这样做的。
在角色管理器上创建一个扩展方法,将 ApplicationRole 替换为您为角色使用的任何 class 名称。
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;
}
}
这是 CheckBoxItem POCO:
public class CheckBoxItem
{
public bool Selected { get; set; }
public string Id { get; set; }
public string Name { get; set; }
}
在您的控制器中获取角色列表并传入所选角色:
var selectedRoles = await _userManager.GetRolesAsync(user);
List<CheckBoxItem> roles = _roleManager.GetRolesSelectList(selectedRoles);
在您的视图中,您可以像这样创建复选框:
@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" />
}
我需要显示一组复选框才能管理用户角色。 我可以给出所有现有角色的观点:
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new MlmDbContext()));
ViewBag.allRoles = roleManager.Roles.ToList();
我可以将用户的角色添加到视图中:
@{
foreach (var role in Model.Roles)
{
但是我应该怎么做才能显示绑定到 Model.Roles 的所有角色而不是保存更改?
你将角色的 id 作为值放在表单上,然后收到 table 所选角色 il controller
在我的一个项目中,这是您应该感兴趣的代码的一部分:
向视图提供数据的控制器:
public async Task<ActionResult> Create()
{
// Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
return View();
}
显示角色的视图:
<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>
接收所选角色的控制器:
[HttpPost]
public async Task<ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
{
if (ModelState.IsValid)
{...}
}
编辑:
好的,这就是我认为您需要的:
在控制器中:
RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
{
Selected = userRoles.Contains(x.Name),
Text = x.Name,
Value = x.Name
}),
在视图中:
<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>
我是这样做的。
在角色管理器上创建一个扩展方法,将 ApplicationRole 替换为您为角色使用的任何 class 名称。
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;
}
}
这是 CheckBoxItem POCO:
public class CheckBoxItem
{
public bool Selected { get; set; }
public string Id { get; set; }
public string Name { get; set; }
}
在您的控制器中获取角色列表并传入所选角色:
var selectedRoles = await _userManager.GetRolesAsync(user);
List<CheckBoxItem> roles = _roleManager.GetRolesSelectList(selectedRoles);
在您的视图中,您可以像这样创建复选框:
@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" />
}