当前位置: 动力学知识库 > 问答 > 编程问答 >

linq - ASP.NET Identity UserRole Table

问题描述:

I am working on getting an ASP ListView control to populate Users based on the value from a DropDownList Control. On page load the list view will be populated with all users in the table. If I select a Role from the DropDownList I want it to display those users that belong to the selected role. I am using ASP Identity 2.0 and Role based authentication, with model binding. I need to create a join using LinQ that joins the User Table, The UserRole Table and the Role table, and I am kind of stuck. If I select a role nothing return back. The code is below.

This snippet binds the Roles to the DropDownList

 public IQueryable<IdentityRole> BindRoles()

{

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(_db));

var roles = from role in roleManager.Roles

orderby role.Name

select role;

return roles;

}

And this is the GetData Method for updating the ListView control based on the selection from the DropDownList

 public IQueryable<User> GetData([Control("RolesDropDown")] string Id)

{

if (Id != null)

{

var users = from u in _db.Users

where u.Id == Id

select u;

return users;

}

else

return _db.Users;

}

Take in to consideration that I am new to Linq and trying to get this to work.

Will.

网友答案:

I cannot seem to find the schema online to reference but there is more then likely a cross reference table that would display the users in there roles.

The syntax you are using as your parameter for GetData([Control("RolesDropDown")] string Id) is new to me. I have never seen that. But if that was the ID of the ROLE you are looking to filter by then you would use that in your query below.

I would get the value from the drop down using RolesDropDown.SelectedValue

 public IQueryable<User> GetData([Control("RolesDropDown")] string Id)
    {

        if (Id != null)
        {
            var users = from u in _db.Users
                        join r in _db.UsersInRoles on u.UserID equals r.UserID
                        where r.RoleID == Id
                        select u;
            return users;
        }
        else
        return _db.Users;
    }

So look for your cross reference table of Roles and Users.

分享给朋友:
您可能感兴趣的文章:
随机阅读: