MVC3 中的分页列表显示错误对象引用未设置为对象的实例



这是我的观点

@using(@Html.BeginForm("CrmBlogGroupType","knowledge",FormMethod.Get)){
       @Html.TextBox("search") 
         @Html.Hidden("type", (string)ViewBag.type)
           @Html.DropDownList("PageSize",
        new List<SelectListItem>()
        {
             new SelectListItem ()
        {
        Text="--Select Page Size--" ,Value="10",Selected=true
        },
        new SelectListItem ()
        {
        Text="View 20 records" ,Value="20"
        },
        new SelectListItem ()
        {
        Text="View 50 records" ,Value="50"
        },
          new SelectListItem ()
        {
        Text="View 100 records" ,Value="100"
        },
        })
           <input type="submit" value="search" id="Searchbtn" />
         <br />
           @Html.CheckBox("Name")<text>Author Name</text>
           @Html.CheckBox("AuthorTitle")<text>Title</text>
           @Html.CheckBox("Description")<text>Description</text>
       }

这是分页列表代码

@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType", 
new     {page,Name=Request.QueryString["Name"].ToLower().Contains("true"),
AuthorTitle=Request.QueryString["AuthorTitle"].ToLower().Contains("true"),
Description=Request.QueryString["Description"].ToLower().Contains("true"),      search=Request.QueryString["search"],PageSize=Request.QueryString["PageSize"],type=Request.QueryStrin g["type"]}),new PagedListRenderOptions() 
{ 
   DisplayLinkToFirstPage=true,DisplayLinkToLastPage=true,DisplayPageCountAndCurrentLocation=true,Displa      yItemSliceAndTotal=true
    ,DisplayEllipsesWhenNotShowingAllPageNumbers=true,MaximumPageNumbersToDisplay=10
})

控制器代码

public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool?Description, string search, int? PageSize, string type)
    {
        if (type==null)
        {
            //setting the Value in the initial call 
            //If the SP has changed then make the type parameter as the INT
            type = "A";
        }
        IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = _dataLayer.GetBlogSet(type).ToList().ToPagedList(page ?? 1, PageSize ?? 10);
        return View(_objBlogSet);
        }

收到错误:

对象引用未设置为对象的实例。

Line 202:    @if (ViewBag.Search!=null && ViewBag.Search!=string.Empty)            
Line 203:{
Line 204:@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType", new { page,
Line        205:Name=Request.QueryString["Name"].ToLower().Contains("true"),AuthorTitle=Request.QueryString["Auth    orTitle"].ToLower().Contains("true"),
Line 206:Description=Request.QueryString["Description"].ToLower().Contains("true"),

我已经通过了一些链接,通过这些链接我可以像这样编写代码,最后卡在这里非常感谢对此的任何帮助。

使用 ViewBag 将各种参数传递给 PagedListPager 。计算控制器中的值,并且视图中没有复杂的逻辑。当控制器具有这些参数的强类型值时,从querystring中提取参数是不必要的重复工作。

public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool?Description, string search, int? PageSize, string type)
{
    // Get the current values (or defaults == false) for the sorting
    ViewBag.Name = Name.GetValueOrDefault();
    ViewBag.AuthorTitle = AuthorTitle.GetValueOrDefault();
    ViewBag.Description= Description.GetValueOrDefault();

并在视图中使用它们,如下所示:

@Html.PagedListPager(Model, page => Url.Action("CrmBlogGroupType", 
    new {page, Name=ViewBag.Name, AuthorTitle=ViewBag.AuthorTitle, Description=ViewBag.Description

更新:10,000 条记录目前很慢

从下面的评论来看,当前的分页很慢。这是因为以下行中的ToList()会导致在将任何分页应用于 LINQ 查询之前返回所有记录。

IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = 
        _dataLayer.GetBlogSet(type)
        .ToList()             // <<<< THIS IS THE CULPRIT
        .ToPagedList(page ?? 1, PageSize ?? 10);

ToPagedList旨在使用IQueryable,以便在向查询添加Skip(n)Take(n)时,它将有效地仅返回记录的页数。只需删除ToList()

IEnumerable<Usp_getBlogSetPosts_Result> _objBlogSet = 
        _dataLayer.GetBlogSet(type)
        .ToPagedList(page ?? 1, PageSize ?? 10);

最新更新