必须绑定数据源才能在MVC 5上执行此操作

时间:2016-11-29 10:47:59

标签: c# asp.net-mvc razor model-view-controller

我正在尝试在视图上显示网格,它在网格上给我一个错误。这是视图

@{var grid = new WebGrid(Model, canPage: true, rowsPerPage: 20,
      selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
     grid.Pager(WebGridPagerModes.NextPrevious);
     ViewBag.Title = "Data results";
}

@{
    ViewBag.Title = "Results";
    Layout = "~/Views/Shared/_InternalLayout.cshtml";
}

<h2>Data Results</h2>
<div id="gridContent">
    <p class="text-success">@ViewBag.Result</p>
    <p class="text-danger">@Html.ValidationMessage("Error")</p>

@using (Html.BeginForm("ReportTable", "ControllerName", FormMethod.Get))
{
    @grid.GetHtml(

   tableStyle: "webGrid2 ReportTable",
                         headerStyle: "header",
                         alternatingRowStyle: "alt",
                         selectedRowStyle: "select",
                         columns: grid.Columns(
                                  grid.Column("Number", "Destination", canSort: false),
                                  grid.Column("Received", "Receive Time", canSort: false),
                                  grid.Column("Message", "Message", canSort: false),
                                  grid.Column("Resubmit", format: @<text><input class="chkbox" id="resubChkbx" name="ResubmitChkbx" type="checkbox" value="@item.ID" /></text>)
                        ))}
</div>

这是控制器

public ActionResult ReportTable()
        {
            return View("ReportTable");

}

我得到的错误代码如下:

  

必须先绑定数据源才能执行此操作

并突出显示@ grid.Gethtml

1 个答案:

答案 0 :(得分:1)

您没有传递WebGrid的来源。

public ActionResult ReportTable()
{
  var yourmodel=new List<YouModelType>(); /// generate model
  return View(yourmodel);//// pass model parameter to view
}