1

描述

我无法让 WebGrid 排序工作。NullReferenceException当视图尝试加载时,它会不断抛出。

环境

WebGrid 2.0.0.0、VisualStudio 2012、MVC 4.0 版

细节

我的控制器操作尚未尝试进行任何排序。我试图通过单击列标题来模拟排序,控制器的索引操作执行时没有任何错误。当视图尝试加载时,出现运行时错误NullReferenceException was unhandled by user code

在调试模式下,我可以将错误追溯到 WebGridGetHtml调用中尝试引用 Rows 对象的某些代码'grid2.Rows' threw an exception of type 'System.NullReferenceException'

我正在使用强类型集合来伪造控制器中的一些数据,将其添加到模型中并将模型提供给视图。

我尝试了许多不同的变体,但这是我的代码的当前状态:

控制器动作

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 up some data

        for (int i = 0; i < 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);

    }

风景

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

))

这是堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an 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

我渴望在这个项目中使用 MVC,但我必须能够快速轻松地构建可搜索、可排序和可分页的网格。

4

1 回答 1

0

我更改了控制器的 index 方法以返回 ProjectCount 对象列表而不是强类型集合,现在一切正常。

这是控制器索引方法的新代码。

public ActionResult Index(string sort, string sortdir)
    {
        List<ProjectCount> items = new List<ProjectCount>();

        for (int i = 0; i < 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);

    }
于 2013-07-24T13:37:20.367 回答