Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to Paging the Index Page implemented by ViewModel

$
0
0

I don't know hot to paging the ViewModel based Index page

1. ViewModel in Index Page

  I created ViewModel because Index Page needs several tables.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Nugu3.Models.NuguModels
{
    public class PeopleIndexData
    {
        public IEnumerable<Person> People { get; set; }
        public IEnumerable<College> Colleges { get; set; }
        public IEnumerable<HighSchool> Highschools { get; set; }
    }
}

2. Paging functionality

and I make paging functionality just like the sample document

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;


public class PaginatedList<T> : List<T>
{
    public int PageIndex {get; private set; }
    public int TotalPages {get; private set; }

    public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);

        this.AddRange(items);
    }

    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 1);
        }
    }

    public bool HasNexPage
    {
        get
        {
            return (PageIndex < TotalPages);
        }
    }

    public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
    {
        var count = await source.CountAsync();
        var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        return new PaginatedList<T>(items, count, pageIndex, pageSize);

    }

}

3. In index Method

not in PeopleController.cs, the PaginatedList can't receive my ViewModel

my source is llike this

        // GET: People Index
        public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, string searchSchool, int? page)
        {
            ViewData["CurrentSort"] = sortOrder;
            ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["FindSchool"] = searchSchool;
            ViewData["DateSortParam"] = sortOrder == "Date" ? "date_desc" : "Date";

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

            ViewData["currentFilter"] = searchString;

            var viewModel = new PeopleIndexData();


            if (!String.IsNullOrEmpty(searchString))
            {
                viewModel.People = viewModel.People.Where(s => s.Name.Contains(searchString));
            }

            if (!String.IsNullOrEmpty(searchSchool))
            {
                viewModel.People = viewModel.People.Where(s => s.College.CollegeName.Contains(searchSchool) || s.HighSchool.SchoolName.Contains(searchSchool));
            }


            switch (sortOrder)
            {
                case "name_desc":
                    viewModel.People = viewModel.People.OrderByDescending(s => s.Name);
                    break;
                default:
                    viewModel.People = viewModel.People.OrderBy(s => s.Name);
                    break;
            }

            int pageSize = 3;

            return View(await PaginatedList<PeopleIndexData>.CreateAsync(viewModel.People.AsQueryable() , page ?? 1, pageSize));
        }

The error message is like this :

  You can't cast from

       <'System.Linq.IQueryable<Nugu3.Models.Person>' to 'System.Linq.IQueryable<Nugu3.Models.NuguModels.PeopleIndexData>'

Does anybody ever solved this problem?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>