在创建用户视图ASP Identity MVC中添加角色

本文关键字:MVC 添加 角色 Identity ASP 创建 用户 视图 | 更新日期: 2023-09-27 18:26:36

我正在写一个项目来管理asp.net身份2.0的用户池,我想在同一视图中创建一个用户并向用户添加角色,所以当我发布模型时,只需简单地创建用户并在同一操作中添加角色。我不知道如何为用户生成角色列表,例如,我有一个包含角色和id的下拉列表,我的视图类似于

<div class="form-horizontal">
<div class="col-md-6">
    <div class="panel panel-info">
        <div class="panel-heading">Datos personales</div>
        <div class="panel-body">
            <div class="form-group">
                <div class="col-md-4">
                    @Html.DisplayNameFor(m => m.User.Nombre)
                </div>
                <div class="col-md-8">
                    @Html.TextBoxFor(m => m.User.Nombre, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4">
                    @Html.DisplayNameFor(m => m.User.Apellido)
                </div>
                <div class="col-md-8">
                    @Html.TextBoxFor(m => m.User.Apellido, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4">
                    @Html.DisplayNameFor(m => m.User.DependenciaId)
                </div>
                <div class="col-md-8">
                    @Html.DropDownListFor(m => m.User.DependenciaId, new SelectList(ViewBag.ListaDependencia, "Id", "Descripcion"), "Ninguno", new { @class = "form-control" })
                </div>
            </div>
        </div>
    </div>
</div>
<div class="col-md-6">
    <div class="panel panel-info">
        <div class="panel-heading">Datos usuario</div>
        <div class="panel-body">
            <div class="form-group">
                <div class="col-md-4">
                    @Html.DisplayNameFor(m => m.User.UserName)
                </div>
                <div class="col-md-8">
                    @Html.TextBoxFor(m => m.User.UserName, new { @class = "form-control" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4">
                    @Html.DisplayNameFor(m => m.User.NetUser)
                </div>
                <div class="col-md-8">
                    @Html.TextBoxFor(m => m.User.NetUser, new { @class = "form-control" })
                </div>
            </div>
        </div>
    </div>
</div>
<div class="panel panel-info">
    <div class="panel-heading">Datos usuario</div>
    <div class="panel-body">
        <div class="form-group">
            <div class="col-md-4">
                @Html.DisplayNameFor(m => m.User.Roles)
            </div>
            <div class="col-md-8">
                @Html.DropDownListFor(m => m.User.Roles, new SelectList(ViewBag.RolesList, "Id", "Name"), "Ninguno", new { @class = "form-control" })
            </div>
        </div>
        <table>
            Here must present the roles to add to the user
        </table>
    </div>
</div>

在创建用户视图ASP Identity MVC中添加角色

我找到了的方法

首先是离线收藏的助手,我在互联网上找到了

public static class HtmlPrefixScopeExtensions
{
    private const string idsToReuseKey = "__htmlPrefixScopeExtensions_IdsToReuse_";
    public static IDisposable BeginCollectionItem(this HtmlHelper html, string collectionName)
    {
        if (html.ViewData["ContainerPrefix"] != null)
        {
            collectionName = string.Concat(html.ViewData["ContainerPrefix"], ".", collectionName);
        }
        var idsToReuse = GetIdsToReuse(html.ViewContext.HttpContext, collectionName);
        var itemIndex = idsToReuse.Count > 0 ? idsToReuse.Dequeue() : Guid.NewGuid().ToString();
        var htmlFieldPrefix = string.Format("{0}[{1}]", collectionName, itemIndex);
        html.ViewData["ContainerPrefix"] = htmlFieldPrefix;
        // autocomplete="off" is needed to work around a very annoying Chrome behaviour whereby it reuses old values after the user clicks "Back", which causes the xyz.index and xyz[...] values to get out of sync.
        html.ViewContext.Writer.WriteLine("<input type='"hidden'" name='"{0}.index'" autocomplete='"off'" value='"{1}'" />", collectionName, html.Encode(itemIndex));
        return BeginHtmlFieldPrefixScope(html, htmlFieldPrefix);
    }
    public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
    {
        return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
    }
    private static Queue<string> GetIdsToReuse(HttpContextBase httpContext, string collectionName)
    {
        // We need to use the same sequence of IDs following a server-side validation failure,  
        // otherwise the framework won't render the validation error messages next to each item.
        var key = idsToReuseKey + collectionName;
        var queue = (Queue<string>)httpContext.Items[key];
        if (queue == null)
        {
            httpContext.Items[key] = queue = new Queue<string>();
            var previouslyUsedIds = httpContext.Request[collectionName + ".index"];
            if (!string.IsNullOrEmpty(previouslyUsedIds))
                foreach (var previouslyUsedId in previouslyUsedIds.Split(','))
                    queue.Enqueue(previouslyUsedId);
        }
        return queue;
    }
    private class HtmlFieldPrefixScope : IDisposable
    {
        private readonly TemplateInfo templateInfo;
        private readonly string previousHtmlFieldPrefix;
        public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
        {
            this.templateInfo = templateInfo;
            previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
            templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
        }
        public void Dispose()
        {
            templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
        }
    }

然后使用编辑器模板

<tr>
@using (Html.BeginCollectionItem("ListaObraSocialPrepagasSeleccionadas"))
{
    <td>
        <input type="radio" name="RolesUserTableRadio" />
        @Html.HiddenFor(model => model.Id, new { @readonly = "readonly" })
    </td>
    <td>
        @Html.HiddenFor(model => model.Id, new { @readonly = "readonly" })
        @Html.DisplayTextFor(model => model.Name)
    </td>
}

管理列表的局部视图

    <script type="text/javascript">
    $(document).ready(function () {
        $("#btnAddRoles").click(function () {
            var rolId = $("#ddRoles").val();
            if (rolId == null || rolId == '') {
                alert("Debe seleccionar un rol.");
                return;
            }
            var foundRol = $("#RolesUserTable").find("input[value='" + rolId + "']");
            if (foundRol.size() > 0) {
                alert("Ya se ha agregado el rol.");
                return;
            }

            $.ajax({
                url: '@Url.Action("AddRoles", "Users")',
                data: {
                    rolId: rolId
                },
                type: 'GET',
                contentType: 'application/x-www-form-urlencoded',
                success: function (data) {
                    if (data.Valid) {
                        $("#RolesUserTable").append(data.html);
                    } else {
                        alert('El rol seleccionado no existe');
                    }
                },
                error: function (jqXHR, exception) {
                    alert('Error durante la llamada al servidor.' + jqXHR.responseText);
                },
                complete: function () {
                }
            });
        });

        $("#btnDeleteRoles").click(function () {
            var myRadio = $('input[name=RolesUserTableRadio]');
            var radio = myRadio.filter(':checked');
            if (radio.size() == 0) {
                alert("Debe seleccionar un rol.");
                return;
            }
            if (!confirm("¿Confirma que desea eliminar el rol seleccionado?")) {
                return;
            }
            $(radio).closest('tr').remove();
        });
    });
</script>
<div style="width: 100%; overflow-x: auto;">
    <table id="RolesUserTable" class="table table-striped">
        <thead>
            <tr>
                <th></th>
                <th>Rol</th>
            </tr>
        </thead>

        @Html.EditorFor(m => m.Roles)
        </table>
    </div>

最后是下拉列表和表格

<div class="form-group">
                <label for="ddRoles" class="col-sm-2 control-label">Roles</label>
                <div class="col-sm-3">
                    @Html.DropDownList("ddRoles", new SelectList(ViewBag.Roleslist, "Id", "Name", null), "Seleccione un rol", new { @class = "selectpicker", data_live_search = "true" })
                </div>
                <div class="btn-group">
                    <button id="btnAddRoles" type="button" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span></button>
                    <button id="btnDeleteRoles" type="button" class="btn btn-default"><span class="glyphicon glyphicon-minus"></span> </button>
                </div>
            </div>
            <div>
                @Html.Partial("_Roles")
            </div>