关于c#:必须先绑定数据源才能在MVC 5上执行此操作

A data source must be bound before this operation can be performed on MVC 5

我正在尝试在视图上显示网格,但在网格上出现错误。这是视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@{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";
}

Data Results

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

这是控制器

1
2
3
4
5
public ActionResult ReportTable()
        {
            return View("ReportTable");

}

我得到的错误代码如下:

A data source must be bound before this operation can be performed

并突出显示@grid.Gethtml


您没有传递 WebGrid 的源代码。

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