Add paging to razor view in asp.net core

asp.net-core c# entity-framework entity-framework-core linq-to-entities

Question

I've been following a simple article I found at microsoft on how ta add paging to a project in c#. This is the article : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-2.1

Now while following this article I had encountered a problem at the end when I want to pass the data to the view. Let me share some of my code so you guys can understand it better :

paginated list which I got from microsft :

    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 HasNextPage
        {
            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);
        }
    }

the method on the controller I use:

    [HttpGet("search/{option?}/{search}")]
    public  IActionResult GetSearchData(string option, string search, int? page, string currentFilter)
    {
        if (search != null)
        {
            page = 1;
        }
        else
        {
            search = currentFilter;
        }
        SearchViewModel data = new SearchViewModel();

        var searchArticle = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles.GetByString(search));
        data.SearchArticle = searchArticle;

        var searchArticle2 = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles2.GetByString(search));
        data.SearchArticle2 = searchArticle2;

        var searchArticle3 = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles3.GetByString(search));
        data.SearchArticle3 = searchArticle3;

        var searchArticle4 = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles4.GetByString(search));
        data.SearchArticle4 = searchArticle4;

        int pageSize = 10;
        //return View("Search", Helpers.PaginatedList<SearchDataItemViewModel>.CreateAsync(data, page ?? 1, pageSize));
        //this is where I got an error
    }

the error I got is :

Argument 1: cannot convert from 'Website.ViewModels.SearchViewModel' to System.Linq.IQueryable< Website.ViewModels.SearchDataItemViewModel >

Also this is an example of one of my querys :

    public IEnumerable<Article2> GetByString(string word)
    {
        IEnumerable<Article2> article2 = _appContext.Article2
            .Include(t => t.ParentPage)
            .Where(t => t.Status == RecordStatus.Enabled && (t.Name.Contains(word) || t.Description.Contains(word)));
        return article2;
    } 

I actually understand what this means, but how can I pass "data" variable on the view so I can use it?
I'm a little bit stuck here so any help guys would be highly appreciated. At least point me in the right direction or something.

Thanks for the time!!!

1
-1
5/1/2018 4:30:08 PM

Accepted Answer

Solved. What I did was modifying the controller this way.

[HttpGet("search/{option?}/{search}")]
public  IActionResult GetSearchData(string option, string search, int? page, string currentFilter)
{
    if (search != null)
    {
        page = 1;
    }
    else
    {
        search = currentFilter;
    }
    SearchViewModel data = new SearchViewModel();
    int pageSize = 10;
    IEnumerable<SearchDataItemViewModel> searchArticle = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles.GetByString(search));
        var searchArticleP = Helpers.PaginatedList<SearchDataItemViewModel>.CreateAsync(searchArticle, page ?? 1, pageSize);
        data.SearchArticle = searchArticleP;

    IEnumerable<SearchDataItemViewModel> searchArticle2 = Mapper.Map<IEnumerable<SearchDataItemViewModel>>(_unitOfWork.Articles2.GetByString(search));
        var searchArticleP2 = Helpers.PaginatedList<SearchDataItemViewModel>.CreateAsync(searchArticle2, page ?? 1, pageSize);
        data.SearchArticle2 = searchArticleP2;
    return View("Search", data));
}
0
5/1/2018 4:25:33 PM

Popular Answer

In your Linq statement you are missing to specify how to Select so it's returning a Linq object; instead of an Article2 object; should look like this:

  public IEnumerable<Article2> GetByString(string word)
{
    IEnumerable<Article2> article2 = _appContext.Article2
        .Include(t => t.ParentPage)
        .Where(t => t.Status == RecordStatus.Enabled && (t.Name.Contains(word) || t.Description.Contains(word))).ToList();
    return article2;
} 

add ToList()



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow