Paging Issue in asp.net MVC

Malam Malam 226 Reputation points
2024-06-09T04:35:25.08+00:00

I am trying to add paging but get the error: CS1061: 'IEnumerable<Subject>' does not contain a definition for 'PageCount' and no accessible extension method 'PageCount' accepting a first argument of type 'IEnumerable<Subject>' could be found (are you missing a using directive or an assembly reference?)

I've tried with PagedList.Mvc NuGet package (Install-Package PagedList.Mvc) but it did not fix the issue. I then tried the X.PageList but it was not able to install. The code below is from

Index.cshtml

@model PagedList.IPagedList<ContosoUniversity.Models.Student>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Students";
}
<h2>Students</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Student", FormMethod.Get))
{
<p>
    Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
    <input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.StudentID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
    </th>
    </th>
    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.FirstMidName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.EnrollmentDate)
    </td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

StudentController.cs

public ViewResult Index(string sortOrder, string searchString)
{

ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";

ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

var students = from s in db.Students

               select s;

if (!String.IsNullOrEmpty(searchString))

{

    students = students.Where(s => s.LastName.Contains(searchString)

                           || s.FirstMidName.Contains(searchString));

}

switch (sortOrder)

{

    case "name_desc":

        students = students.OrderByDescending(s => s.LastName);

        break;

    case "Date":

        students = students.OrderBy(s => s.EnrollmentDate);

        break;

    case "date_desc":

        students = students.OrderByDescending(s => s.EnrollmentDate);

        break;

    default:

        students = students.OrderBy(s => s.LastName);

        break;

}

return View(students.ToList());
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
{count} votes

Accepted answer
  1. Ping Ni-MSFT 3,115 Reputation points Microsoft Vendor
    2024-06-10T03:48:25.66+00:00

    Hi @Malam Malam

    Your shared code seems contain multiple issues, any way i shared a whole working demo about how to use X.PagedList to paging the data.

    Be sure download X.PagedList and X.PagedList.Mvcpackage. Check the packages.config file the package name and version should be like below:

    <package id="X.PagedList" version="7.9.0" targetFramework="net472" />
    <package id="X.PagedList.Mvc" version="8.0.5" targetFramework="net472" />
    

    Model

    public class Student
    {
        public int StudentID { get; set; }
        public string FirstName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public string LastName { get; set; }
    }
    

    Index.cshtml

    @model X.PagedList.IPagedList<DotnetMvcProject.Models.Student>
    @using X.PagedList.Mvc; @*import this so we get the HTML Helper*@
    @{
        ViewBag.Title = "Students";
    }
    <h2>Students</h2>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    @using (Html.BeginForm("Index", "Student", FormMethod.Get))
    {
        <p>
            Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
            <input type="submit" value="Search" />
        </p>
    }
    <table class="table">
        <tr>
            <th>
                @Html.ActionLink("LastName", "Index", new { sortOrder = ViewBag.NameSortParm })
            </th>
            <th>
                FirstMidName
            </th>
            <th>
                @Html.ActionLink("EnrollmentDate", "Index", new { sortOrder = ViewBag.DateSortParm })
            </th>
            <th></th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstMidName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EnrollmentDate)
                </td>
            </tr>
        }
    </table>
    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
    

    Controller

    
    public ViewResult Index(string sortOrder, string searchString,int page=1)
    {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
        var students = from s in db.Students
                        select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            students = students.Where(s => s.LastName.Contains(searchString)
                                    || s.FirstMidName.Contains(searchString));
        }
        switch (sortOrder)
        {
            case "name_desc":
                students = students.OrderByDescending(s => s.LastName);
                break;
            case "Date":
                students = students.OrderBy(s => s.EnrollmentDate);
                break;
            case "date_desc":
                students = students.OrderByDescending(s => s.EnrollmentDate);
                break;
            default:
                students = students.OrderBy(s => s.LastName);
                break;
        }
        //define the size of each page
        const int pageSize = 5;
        var listPaged = students.ToPagedList(page, pageSize); //convert to IPagedList<Student>
        return View(listPaged);
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Rena


1 additional answer

Sort by: Most helpful
  1. SurferOnWww 2,406 Reputation points
    2024-06-10T00:44:25.3733333+00:00

    I suggest that you write pager accoding to the following Microsoft tutorial:

    Tutorial: Add sorting, filtering, and paging - ASP.NET MVC with EF Core

    See Add paging to Students Index section in the above tutorial which shows the source code of PaginatedList<T> class. Consider using PaginatedList<T> class so that you will be able to write your pager code all under your control rather than using NuGet packages for paging.