PagedList in MVC3 showing Error Object reference not set to an instance of an object

1.6k Views Asked by At

This is My View

@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>
       }

Here is the PagedList Code

@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
})

Controller Code

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

Getting an ERROR :

Object reference not set to an instance of an object.

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"),

I've gone through some links by which i could make up the code like this, at last got stuck here Any help on this is highly appreciated..

1

There are 1 best solutions below

7
On BEST ANSWER

Use the ViewBag for passing the various parameters to the PagedListPager. Calculate the values in the controller and don't have complex logic in a view. Pulling parameters from querystring, when the controller has strongly typed values for those, is an unnecessary duplication of effort.

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();

and use them in the view like this:

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

etc

Update: 10,000 records is currently slow

From the comments below the current paging is slow. That is because the ToList() in the following line causes all records to be returned before any paging is applied to the LINQ query.

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

ToPagedList is designed to work an an IQueryable so that when it adds Skip(n) and Take(n) to the query it will efficiently return just the page worth of records. Simply remove the ToList():

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