troygoode / MembershipStarterKit

The starter kit provides the Asp.Net MVC controllers, models, and views needed to administer users & roles.
http://github.com/TroyGoode/MembershipStarterKit
MIT License
198 stars 66 forks source link

Filtering Users by Role #30

Closed rexdefuror closed 12 years ago

rexdefuror commented 12 years ago

I'm still in process of getting full grasp on MVC Membership and this starter kit helped a lot. Now I have an issue which I could not solve on my own.

My user table viewmodel:

public UserTableVM
{
    public IPagedList<MembershipUser> Users {get; set;}
    public IEnumerable<string> Roles {get; set;}
}

And when I try to filter users by role it won't work because I get IEnumerable instead of IPagedList, I've tried casting it and get bunch of errors, also I've tried changing the type in viewmodel to IEnumerable instead of IPagedList and still it errors out.

This is the code I've tried.

string[] roles = Roles.GetRolesForUser();
return View(new UserTableVM
{
    Users = _userService
        .FindAll(index ?? 0, PageSize)
        .Where(
            x => _rolesService.GetRolesForUser(x.UserName).SequenceEqual(roles)
        ),
    Roles = roles
});

Please help.

troygoode commented 12 years ago

This project relies upon my PagedList project (https://github.com/TroyGoode/PagedList), and expects the Users property to implement IPagedList. Your example should be more like so:

string[] roles = Roles.GetRolesForUser();
return View(new UserTableVM
{
    Users = _userService
        .Where(
            x => _rolesService.GetRolesForUser(x.UserName).SequenceEqual(roles)
        ).ToPagedList(index ?? 0, PageSize), // <-- CONVERT TO IPAGEDLIST<T>
    Roles = roles
});