0

Couldn't really find much about this when I tried searching online, but pagination doesn't seem to be working using ViewModels. From what I can understand Pagination can only be applied to models not viewmodels, so does that mean we have to use the Paging namespace on the ViewModels properties instead? If so what would that look like?

Action Method:

 [HttpGet]
    public ActionResult TranscriptAdmin(int id, string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        StudentMajorRepo sRepo;
        MajorRequirmentsRepo mRepo;
        GradesRepo gRepo;
        RegistrationRepo rRepo;
        TranscriptViewModel viewModel = new TranscriptViewModel();
        viewModel.StudentId = id;
        IList<Grade> AllClasses = new List<Grade>();
        IList<Registration> AllRegistrations = new List<Registration>();
        IList<Registration> PendingGrades = new List<Registration>();

        using (context)
        {
            sRepo = new StudentMajorRepo(context);
            mRepo = new MajorRequirmentsRepo(context);
            gRepo = new GradesRepo(context);
            rRepo = new RegistrationRepo(context);

            viewModel.AllGradesClasses = gRepo.GetAllGradesByUserId(id);
            viewModel.StudentMajor = sRepo.GetByStudentId(id);

        }

        if (!String.IsNullOrEmpty(searchString))
        {
            viewModel.AllGradesClasses = viewModel.AllGradesClasses.Where(s => s.Registration.CourseSection.Course.CourseTitle.Contains(searchString)).ToList();
        }
        switch (sortOrder)
        {
            case "name_desc":
                viewModel.AllGradesClasses = viewModel.AllGradesClasses.OrderByDescending(c => c.Registration.CourseSection.Course.CourseTitle).ToList();
                break;
            
            default:  // Name ascending 
                break;
        }


        int pageSize = 3;
        int pageNumber = (page ?? 1);
        viewModel.AllGradesClasses.ToPagedList(pageNumber, pageSize);
        return View(viewModel);
    }

ViewModel:

 public class TranscriptViewModel
{
    public TranscriptViewModel()
    {
        AllGradesClasses = new List<Grade>();

    }
    public int TotalCredits { get; set; }
    public float TermGpa { get; set; }
    public float CumulativeGpa { get; set; }
    public int PeriodId { get; set; }
    public int StudentId { get; set; }
    public User Student { get; set; }
    public IList<SemesterPeriod> SemesterPeriods { get; set; }
    public StudentMajor StudentMajor { get; set; }
    public IList<MajorRequirements> MajorRequirements { get; set; }
    public IList<Grade> AllGradesClasses { get; set; }

    public SelectList PeriodList
    {
        get
        {
            return new SelectList(SemesterPeriods, "Id", "Period.Value");
        }
    }

    public void PopulateSelectList(IList<SemesterPeriod> populatedPeriods)
    {
        SemesterPeriods = populatedPeriods;

    }
}

Razor page:

    @model BryantUniversity.ViewModels.TranscriptViewModel
    @using PagedList
    @using PagedList.Mvc
    
    @{
        ViewBag.Title = "Transcript";
    }
    
    <div class="container">
        <div class="row">
            <div class="col-sm">
    
            </div>
            <div class="col-md-6">
                @using (Html.BeginForm())
                {
                    <p>
                        Find by name: @Html.TextBox("SearchString")
                        <input type="submit" value="Search" />
                    </p>
                }
            </div>
            <div class="col-sm">
    
            </div>
        </div>
    
        <div class="row">
            <div class="col-sm">
                <h1>Major: @Model.StudentMajor.Major.Title</h1>
    
            </div>
            <div class="col-md-6">
                <table class="table table-striped table-dark">
                    <thead class="thead-dark">
                        <tr>
                            <th>Course Id</th>
                            <th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId, sortOrder = ViewBag.NameSortParm }, null)</th>
                            <th>Credits</th>
                            <th>Grade</th>
                            <th>Semester</th>
                        </tr>
                    </thead>
                    <tbody id="myTable">
                        @foreach (var gradeRegistration in Model.AllGradesClasses)
                        {
                            <tr>
                                <td>@gradeRegistration.Registration.CourseSection.Course.CourseTitleId</td>
                                <td>@gradeRegistration.Registration.CourseSection.Course.CourseTitle</td>
                                <td>@gradeRegistration.Registration.CourseSection.Course.Credits</td>
                                @if (gradeRegistration.LetterGrade == null)
                                {
                                    <td>
                                        Pending
                                    </td>
                                }
                                else
                                {
                                    <td>
                                        @gradeRegistration.LetterGrade.GradeVal.Value
                                    </td>
                                }
                                <td>@gradeRegistration.Registration.CourseSection.SemesterPeriod.Period.Value</td>
                            </tr>
                        }
                    </tbody>
        

</table>
  • What exactly doesn't work? We need more details to help you. A View Model is simply a "model" for use in a view -- nothing fundamentally different. The PagedList should work if your "model" collection implements IQueryable or IEnumerable (IList inherits IEnumerable). Furthermore, you return the original viewModel instead of the result of the pagination. – Jasen Mar 10 '22 at 21:22
  • The above code doesn't produce pagination in that view, it still lists all the items as a giant list. I'm not sure what you mean by I must return the original viewModel instead of the result of the pagination. – Sarthak Thakur Mar 10 '22 at 21:37
  • `viewModel.AllGradesClasses.ToPagedList(pageNumber, pageSize); return View(viewModel);` This looks like you paginate but don't store the result; then return the original set. – Jasen Mar 10 '22 at 22:15
  • Well in order to do that I would need to change my ViewModel AllGradesClass property type to IPagedList, which is why I asked is this something that is required when trying to make pagination work with ViewModels? And if we must chnage that property type then does that mean our code in the controller and view look different? As far as I could find on the Microsoft documentation they only give examples of pagination with model properties not what the code would look like with ViewModels – Sarthak Thakur Mar 11 '22 at 13:53
  • Then do a projection instead of a direct query, and paginate that collection. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/projection-operations – Jasen Mar 11 '22 at 18:16
  • 1
    Again, if your model (or view model) class is just a data-bag with properties and no methods, then the only difference is contextual usage. A paged list doesn't know or care if you're paginating a list of models, or view models, or T. It's generic and the algorithm behaves the same way. – Jasen Mar 11 '22 at 18:31
  • See https://stackoverflow.com/questions/25125329/using-a-pagedlist-with-a-viewmodel-asp-net-mvc and https://stackoverflow.com/questions/26596648/how-to-pass-viewmodel-to-pagedlist. Both are mapping to view models. – Jasen Mar 11 '22 at 18:32
  • Got you, this definitely helped, I just had to change my AllGradeClasses type from IList to IPagedList. I didn't end up using the projection query as I'm using Entityframework. – Sarthak Thakur Mar 14 '22 at 18:15

0 Answers0