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

时间:2014-10-15 20:07:18

标签: c# asp.net asp.net-mvc asp.net-identity roles

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

1 个答案:

答案 0 :(得分:1)

我找到了一种方式

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

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>