Search code examples
c#asp.net-mvcasp.net-mvc-viewmodelpagedlist

Using a PagedList with a ViewModel ASP.Net MVC


I'm trying to using a PagedList in my ASP.Net application and I found this example on the Microsoft website http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

How is it possible to use a PagedList in a complex situation that uses a ViewModel? I'm trying to add a PagedList without success to the Instructor example posted here: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

The problem is that the ViewModel is composed by classes and not simple fields, so I cannot convert the result with the ToPageList() method.

This is the ViewModel structure:

using System.Collections.Generic;
using ContosoUniversity.Models;

namespace ContosoUniversity.ViewModels
{
    public class InstructorIndexData
    {
        public IEnumerable<Instructor> Instructors { get; set; }
        public IEnumerable<Course> Courses { get; set; }
        public IEnumerable<Enrollment> Enrollments { get; set; }
    }
}

I need to join the three tables into the ViewModel and display the result in a View.


Solution

  • I modified the code as follow:

    ViewModel

    using System.Collections.Generic;
    using ContosoUniversity.Models;
    
    namespace ContosoUniversity.ViewModels
    {
        public class InstructorIndexData
        {
         public PagedList.IPagedList<Instructor> Instructors { get; set; }
         public PagedList.IPagedList<Course> Courses { get; set; }
         public PagedList.IPagedList<Enrollment> Enrollments { get; set; }
        }
    }
    

    Controller

    public ActionResult Index(int? id, int? courseID,int? InstructorPage,int? CoursePage,int? EnrollmentPage)
    {
     int instructPageNumber = (InstructorPage?? 1);
     int CoursePageNumber = (CoursePage?? 1);
     int EnrollmentPageNumber = (EnrollmentPage?? 1);
     var viewModel = new InstructorIndexData();
     viewModel.Instructors = db.Instructors
        .Include(i => i.OfficeAssignment)
        .Include(i => i.Courses.Select(c => c.Department))
        .OrderBy(i => i.LastName).ToPagedList(instructPageNumber,5);
    
     if (id != null)
     {
        ViewBag.InstructorID = id.Value;
        viewModel.Courses = viewModel.Instructors.Where(
            i => i.ID == id.Value).Single().Courses.ToPagedList(CoursePageNumber,5);
     }
    
     if (courseID != null)
     {
        ViewBag.CourseID = courseID.Value;
        viewModel.Enrollments = viewModel.Courses.Where(
            x => x.CourseID == courseID).Single().Enrollments.ToPagedList(EnrollmentPageNumber,5);
     }
    
     return View(viewModel);
    }
    

    View

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

    I hope this would help you!!