asp.net mvc - WebGrid.GetHtml() throws Object reference not set to an instance of an object when the -
description
i can not webgrid sort work. keeps throwing nullreferenceexception when view tries load.
environment
webgrid 2.0.0.0, visualstudio 2012, mvc version 4.0
details
my controller action not attempt sorting yet. tried simulate sort clicking on column header, controller's index action executes without errors. when view tries load runtime error nullreferenceexception unhandled user code
in debug mode can trace error code in webgrid's gethtml call attempts reference rows object 'grid2.rows' threw exception of type 'system.nullreferenceexception'
i using typed collection fake data in controller, add model , serve model view.
i have tried number of different variations, here current state of code:
the controller action
public class homecontroller : controller { public actionresult index(string sort, string sortdir) { landingpagedata mydata = new landingpagedata(); mydata.username = "joebagodonuts"; replacementreservemvcdesign.viewmodels.projectcounts<projectcount> currentwork = new projectcounts<projectcount>(); //hacking data (int = 0; < 1000; i++) { projectcount currentcount = new projectcount(); currentcount.projectid = "pa-" + i.tostring(); currentcount.developmentname = "development name " + i.tostring(); currentcount.opencount = i; currentcount.actionrequiredcount = i; currentwork.add(currentcount); } mydata.userprojectcounts = currentwork; return view(mydata.userprojectcounts); } the view
@model ienumerable<projectcount> @using system.web.helpers @using replacementreservemvcdesign.viewmodels @{ viewbag.title = "index"; layout = "~/views/shared/_layout.cshtml"; webgrid grid2 = new webgrid(model, cansort:true); } @grid2.gethtml( columns: grid2.columns( grid2.column(columnname:"projectid", header: "project id"), grid2.column(columnname:"developmentname", header:"development name"), grid2.column(columnname:"opencount", style: "text-align-center", header: "open requests"), grid2.column(columnname:"actionrequiredcount", header: "action required", style: "text-align-center") )) here stack trace:
[nullreferenceexception: object reference not set instance of object.] lambda_method(closure , projectcount ) +81 system.linq.enumerablesorter`2.computekeys(telement[] elements, int32 count) +147 system.linq.enumerablesorter`1.sort(telement[] elements, int32 count) +37 system.linq.<getenumerator>d__0.movenext() +330 system.linq.<skipiterator>d__4d`1.movenext() +397 system.linq.<takeiterator>d__3a`1.movenext() +375 system.collections.generic.list`1..ctor(ienumerable`1 collection) +535 system.linq.enumerable.tolist(ienumerable`1 source) +79 system.web.helpers.webgriddatasource.getrows(sortinfo sortinfo, int32 pageindex) +166 system.web.helpers.webgrid.get_rows() +118 system.web.helpers.<>c__displayclass4.<table>b__3(textwriter __razor_helper_writer) +1191 system.web.webpages.helperresult.tostring() +102 system.web.webpages.webpageexecutingbase.writeto(textwriter writer, object content) +16 asp._page_views_home_index_cshtml.execute() in c:\users\is_rbm\documents\visual studio 2012 \projects\replacementreservemvcdesign\replacementreservemvcdesign\views\home\index.cshtml:14 system.web.webpages.webpagebase.executepagehierarchy() +279 system.web.mvc.webviewpage.executepagehierarchy() +125 system.web.webpages.startpage.executepagehierarchy() +142 system.web.webpages.webpagebase.executepagehierarchy(webpagecontext pagecontext, textwriter writer, webpagerenderingbase startpage) +180 system.web.mvc.viewresultbase.executeresult(controllercontext context) +377 system.web.mvc.<>c__displayclass1a.<invokeactionresultwithfilters>b__17() +32 system.web.mvc.controlleractioninvoker.invokeactionresultfilter(iresultfilter filter, resultexecutingcontext precontext, func`1 continuation) +854204 system.web.mvc.controlleractioninvoker.invokeactionresultwithfilters(controllercontext controllercontext, ilist`1 filters, actionresult actionresult) +265 system.web.mvc.async.<>c__displayclass25.<begininvokeaction>b__22(iasyncresult asyncresult) +838676 system.web.mvc.<>c__displayclass1d.<beginexecutecore>b__18(iasyncresult asyncresult) +28 system.web.mvc.async.<>c__displayclass4.<makevoiddelegate>b__3(iasyncresult ar) +15 system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) +65 system.web.mvc.async.<>c__displayclass4.<makevoiddelegate>b__3(iasyncresult ar) +15 system.web.mvc.controller.endexecute(iasyncresult asyncresult) +51 system.web.mvc.<>c__displayclass8.<beginprocessrequest>b__3(iasyncresult asyncresult) +42 system.web.mvc.async.<>c__displayclass4.<makevoiddelegate>b__3(iasyncresult ar) +15 system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) +51 system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +606 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +288 i eager use mvc in project, have able build searchable,sortable , pageable grids , easily.
i changed controller's index method return list of projectcount objects rather typed collection , work now.
here's new code controller index method.
public actionresult index(string sort, string sortdir) { list<projectcount> items = new list<projectcount>(); (int = 0; < 1000000; i++) { projectcount currentcount = new projectcount(); currentcount.projectid = "pa-" + i.tostring(); currentcount.developmentname = "development name " + i.tostring(); currentcount.opencount = i; currentcount.actionrequiredcount = i; items.add(currentcount); } return view(items); }
Comments
Post a Comment