asp.net mvc - PagedList in MVC3 showing Error Object reference not set to an instance of an object -


this 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 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 value in initial call              //if sp has changed make type parameter int             type = "a";         }          ienumerable<usp_getblogsetposts_result> _objblogset = _datalayer.getblogset(type).tolist().topagedlist(page ?? 1, pagesize ?? 10);         return view(_objblogset);         } 

getting error :

object reference not set instance of 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 links make code this, @ last got stuck here on highly appreciated..

use viewbag passing various parameters pagedlistpager. calculate values in controller , don't have complex logic in view. pulling parameters querystring, when controller has typed values those, unnecessary duplication of effort.

public actionresult crmbloggrouptype(int? page, bool? name, bool? authortitle, bool?description, string search, int? pagesize, string type) {     // current values (or defaults == false) sorting     viewbag.name = name.getvalueordefault();     viewbag.authortitle = authortitle.getvalueordefault();     viewbag.description= description.getvalueordefault(); 

and use them in view this:

@html.pagedlistpager(model, page => url.action("crmbloggrouptype",      new {page, name=viewbag.name, authortitle=viewbag.authortitle, description=viewbag.description 

etc

update: 10,000 records slow

from comments below current paging slow. because tolist() in following line causes records returned before paging applied linq query.

ienumerable<usp_getblogsetposts_result> _objblogset =          _datalayer.getblogset(type)         .tolist()             // <<<< culprit         .topagedlist(page ?? 1, pagesize ?? 10); 

topagedlist designed work an iqueryable when adds skip(n) , take(n) query efficiently return page worth of records. remove tolist():

ienumerable<usp_getblogsetposts_result> _objblogset =          _datalayer.getblogset(type)         .topagedlist(page ?? 1, pagesize ?? 10); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -