Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'PagedList.IPagedList'

asp.net-mvc c# entity-framework-6 pagedlist

Question

I am trying to use pagination for a ASP.NET MVC 5 application. I have used X.PagedList I have implemented it correctly in a simple object, but I have problem when implementing it with a more complex object. For example like when I have relationship like Posts and Comments in a forum.

My models have the following structure

public class PublicReport
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PublicReportID { get; set; }

    public string Title { get; set; }       
    ...
    public virtual Institutions Institution { get; set; }
    ...
}   

public class Institutions
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int InstitutionID { get; set; }
    public string Name { get; set; }
    ...
}

In the controller is the following code

public ActionResult ListReports(int? page)
{
    var pageNumber = page ?? 1;     
    var listOfReports = db.PublicReport.OrderByDescending(d => d.CreatedDate).Where(c => c.CategoryId == 1).ToList();
    listOfReports.ToPagedList(pageNumber, 3);
    return View("ReportList", listOfReports);
}

In the View (PartialView) the relevant part is

@model IEnumerable<RENJK.Models.PublicReport>

@using PagedList.Mvc;
@using PagedList;   

@foreach (var item in Model)
{
    <div class="consult-post-sec">
        <div class="row">
            <div class="col-sm-12">
                <p>@Html.DisplayFor(modelItem => item.Title)</p>
                <div class="consult-posts-by">
                    @Html.DisplayFor(modelItem => item.Institution.Name)
                </div>
            </div>
        </div>
    </div>
}

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }), PagedListRenderOptions.OnlyShowFivePagesAtATime) 

The error that I get is

Unable to cast object of type 'System.Collections.Generic.List`1[RENJK.Models.PublicReport]' to type 'PagedList.IPagedList'.

The problem seems to be in the view in the line of code where i want to display the ListPager because the error wont show if i remove that.

What can I do in this case?

1
1
5/5/2016 9:40:51 AM

Accepted Answer

You are not assigning the results of ToPagedList() to a variable (and therefore not returning IPagedList<PublicReport>, just IEnumerable<PublicReport>). Change your code to

public ActionResult ListReports(int? page)
{
    var pageNumber = page ?? 1;     
    var listOfReports = db.PublicReport.OrderByDescending(d => d.CreatedDate).Where(c => c.CategoryId == 1).ToList();
     var pagedList = listOfReports.ToPagedList(pageNumber, 3); // modify
     return View("ReportList", pagedList ); // modify
 }

Note also your model in the view should be

@model IPagedList<RENJK.Models.PublicReport>

and then there is no need for the case. It can be just

@Html.PagedListPager(Model, page => Url.Action(....`
2
5/5/2016 10:15:51 AM


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