2016-11-29 24 views
0

我試圖在視圖上顯示網格,它給了我一個網格上的錯誤。這是視圖必須先綁定數據源,然後才能在MVC上執行此操作5

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

} 

錯誤代碼,我得到的是這樣的:

數據源必須綁定此操作前可執行

And highlights @ grid.Gethtml

+1

你還沒有告訴網格數據來自 – ADyson

回答

1

您沒有傳遞WebGrid的源代碼。

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

感謝您的回覆......但爲什麼列表而不是新模型? –

+1

@jacopk。因爲WebGrid期望'IEnumerable ' – esiprogrammer

相關問題