繁体   English   中英

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

[英]Adding roles in the create user view ASP Identity MVC

我正在编写一个项目来管理asp.net Identity 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>

我找到了一个方法

首先是在互联网上找到的离线收藏的帮助者

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>

暂无
暂无

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

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