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.Mvc
package. 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