Search code examples
asp.net-mvcasp.net-membershippagedlist

mvc pagedlist asp.net membership


I am using the PagedList.mvc helper with ASP.NET membership to display application user details in a table. The pager works correctly using the Membership.GetAllUsers() method, but I would like to be able to take advantage of the overload that returns a single page of data, rather than the whole table, as suggested in this article:

        int pageSize = int.Parse(ConfigurationManager.AppSettings["gridPageSize"]);
        int totalRecords;
        IEnumerable<MembershipUser> users = Membership.GetAllUsers((page ?? 0), pageSize, out totalRecords).Cast<MembershipUser>();
        return View(users.ToPagedList((page ?? 1), pageSize));

The above code, which uses the overload in question, unfortunately only displays the first page of data, and provides no link to the second or subsequent pages. It seems this is because the PagedList helper configures itself according to the size of the collection passed to it, rather than the actual size of the table (available from the output parameter of GetAllUsers(int, out int).

Does anyone know of a way round this apparent limitation?


Solution

  • Use StaticPagedList, per Example 2 of the PagedList README:

    https://github.com/troygoode/pagedlist#example-2-manual-paging

    var users = Membership.GetAllUsers(pageIndex, pageSize, out totalUserCount);
    var usersAsIPagedList = new StaticPagedList<MembershipUser>(users, pageIndex + 1, pageSize, totalUserCount);