1
votes

I want to make paging in the table using Pagedlist and get an error

The model item passed into the dictionary is of type 'PagedList.PagedList1[System.Collections.Generic.KeyValuePair2[MyProject.Data.Models.User,System.Collections.Generic.IEnumerable`1[MyProject.Data.Models.Role]]]', but this dictionary requires a model item of type

I can make paging but only for one model like using code "View(db.Roles.ToList().ToPagedList(pageNumber, pageSize))". But for this case it's different, here I use two models namely User and Role. I use a dictionary to call the two models. I am confused about how to implement it in view using PagedList. This is the code that I have tried.Thanks

public ActionResult Index(int? page)
{
    int pageSize = 2;
    int pageNumber = (page ?? 1);
    Dictionary<User, IEnumerable<Role>> userRoles = userRoleRepo.GetDictionary_UserAndRolesOfEachUser();
   return View(userRoles.ToPagedList(pageNumber, pageSize));
}

View

 @model PagedList.IPagedList<Dictionary<MyProject.Data.Models.User, IEnumerable<MyProject.Data.Models.Role>>>
 <table class="table">
    <tr class="header">
        <th> UserName</th>
        <th> FullName</th>
    </tr>
     @foreach (Dictionary<MyProject.Data.Models.User, IEnumerable<MyProject.Data.Models.Role>> item in Model)
     {
        <tr class="detail">
            <td>
                @Html.DisplayFor(modelItem => item.First().Key.UserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.First().Key.FullName)
            </td>
            <td>
                <ul>
                    @foreach (var role in item.First().Value)
                    {
                        <li>
                            @Html.DisplayFor(modelItem => role.RoleName)
                        </li>
                    }
                </ul>
            </td>
        </tr>
     }
 </table>

    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Index", new { page}))
1

1 Answers

1
votes

have you tried the following?

@model PagedList.IPagedList<KeyValuePair<MyProject.Data.Models.User, IEnumerable<MyProject.Data.Models.Role>>>