For Solution of above problem, we are creating one student controller and its index view. We want to display a paging number of records(i.e. First Name,Last Name or Enrollment date) in index view. We are also say, it is complete code of searching, sorting ,filtering and paging example in an asp.net mvc 4. For paging we want to download one references i.e. PagedList.
The NuGet PagedList.Mvcpackage automatically installs thePagedListpackage as a dependency. ThePagedListpackage installs a PagedListcollection type and extension methods for IQueryableand IEnumerable collections. From theToolsmenu, selectLibrary Package Managerand thenManage NuGet Packages for Solution.
this is a code of Student Controller Index Action:
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page) { ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; var students = from s in db.Student select s; if (!String.IsNullOrEmpty(searchString)) { students = students.Where(s => s.FirstName.ToUpper().Contains (searchString.ToUpper()) ||s.LastName.ToUpper().Contains(searchString.ToUpper())); } 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: // Name ascending students = students.OrderBy(s => s.LastName); break; }
Now we will change in a student index view. code is as follows:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The NuGet PagedList.Mvc package automatically installs the PagedList package as a dependency. The PagedList package installs a PagedList collection type and extension methods for IQueryable and IEnumerable collections. From the Tools menu, select Library Package Manager and then Manage NuGet Packages for Solution.