使用角色名称在GridView中显示用户列表

时间:2015-08-29 09:23:21

标签: asp.net asp.net-identity-2

我想在GridView控件中显示用户列表,我希望有一个列显示每个用户的角色名称。 如何在GridView控件中显示用户角色? GridView标记:

<asp:GridView runat="server" ID="grdUsers" AllowPaging="True" PageSize="20"
                                ItemType="EStore.Models.ApplicationUser"
                                AutoGenerateColumns="False" CssClass="table table-striped table-advance table-hover"
                                OnRowCommand="grdUsers_OnRowCommand">
                                <Columns>
                                    <asp:BoundField DataField="FullName" HeaderText="Full Name" />
                                    <asp:BoundField DataField="UserName" HeaderText="User Name" />
                                    <asp:BoundField DataField="Email" HeaderText="Email" />
                                    <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" />
                                    <asp:BoundField DataField="Roles" HeaderText="Role Name" />
                                    <asp:TemplateField HeaderText="Commands">
                                        <ItemTemplate>
                                            <asp:LinkButton runat="server" ID="lbEditUser" CssClass="btn btn-primary btn-xs"
                                                ToolTip="Edit User" CommandArgument='<%#Eval("Id") %>'
                                                CommandName="EditUser">
                                                <i class="icon-pencil"></i>
                                            </asp:LinkButton>
                                            <asp:LinkButton runat="server" ID="lbDeleteUser" CssClass="btn btn-danger btn-xs"
                                                ToolTip="Delete User" CommandArgument='<%#Eval("Id") %>'
                                                CommandName="DeleteUser">
                                                <i class="icon-trash"></i>
                                            </asp:LinkButton>
                                            <asp:LinkButton runat="server" ID="lbChangeRole" CssClass="btn btn-danger btn-xs"
                                                ToolTip="Change User Role" CommandArgument='<%#Eval("Id") %>'
                                                CommandName="ChangeRole">
                                                <i class="icon-folder-open"></i>
                                            </asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

C#代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                grdUsers.DataSource = GetUsers().OrderByDescending(u => u.RegisteredDate).ToList();
                grdUsers.DataBind();
            }
        }

        public IQueryable<ApplicationUser> GetUsers(string key = null)
        {
            var _db = new ApplicationDbContext();
            IQueryable<ApplicationUser> query = _db.Users;
            if (key != null)
            {
                query = query.Where(u => u.UserName.Contains(key) || u.FullName.Contains(key) || u.Email.Contains(key));
            }
            return query;
        } 

在代码中,我检索用户列表,但角色列不显示用户角色。 在此应用程序中,每个用户都属于一个角色。

0 个答案:

没有答案