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

大憨熊 提交于 2019-12-11 12:15:42

问题


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:


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(....`


来源:https://stackoverflow.com/questions/37047263/unable-to-cast-object-of-type-system-data-entity-infrastructure-dbquery-to-typ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!