@{
List<Customer> Custs =
new
List<Customer>();
Custs.Add(
new
Customer { CustomerCode =
"1001"
, CustomerName =
"Shiv"
});
Custs.Add(
new
Customer { CustomerCode =
"1002"
, CustomerName =
"Shiv1"
});
|
要在 MVC 的視圖(View) 中顯示集合的內容, 咱們先建立一個 「WebGrid」 對象, 傳給 "WebGrid" 構造函數的變量 」Custs」 是上面代碼建立的 "Customer" 對象列表 .html
接下來, 調用 「GetHtml()」 函數, 以 HTML 表格形式顯示集合中的內容.java
1
2
3
4
|
@{
WebGrid obj =
new
WebGrid(Custs);
}
@obj
.GetHtml();
|
<% var grid = new WebGrid(source: Model.List1, defaultSort: "Name", rowsPerPage: 5); %>
<% var grid = new WebGrid(source: Model.List2, defaultSort: "Name", rowsPerPage: 5); %>
pulic class indexmodel{
public list<xx> List1{get;set}
public list<xx> List1{get;set}
}
2 用 viewdata["xx"]=xxlist;
<% var grid = new WebGrid(source:viewdata["xx"] as Listxx , defaultSort: "Name", rowsPerPage: 5); %>
ajax
@model List < WebGridRazor.Models.Products >
or 函數
@model IList<Android.Models.Contact>
this
屬性解釋spa
1 DataSource 獲取綁定到的WebGrid的數據源code
2 TotalRowCount 獲取綁定到WebGrid的數據總行數htm
3 PageIndex 獲取WebGrid總頁數對象
4 SortDirection 獲取或者設置WebGrid的排序方向blog
5 SelectedIndex 獲取WebGrid的選擇行的index
public ActionResult Gridview()
2 {
3 DALDataContext da = new DALDataContext();
4 var result =da.T_STUDENTs.ToList();
5 this.ViewData.Model = result;
6 return View();
7 }
1 @using MVC3Tutorial;
2 @model List<T_STUDENT>
3 @{
4 View.Title = "Gridview";
5 Layout = "~/Views/Shared/_Layout.cshtml";
6 }
7
8 @{
9 var grid = new WebGrid(source:Model,
10 fieldNamePrefix:"grid_",
11 defaultSort: "StudentName",
12 canPage:true,
13 canSort:true,
14 ajaxUpdateContainerId:"DivGrid",
15 pageFieldName:"paging",
16 sortFieldName:"sortField",
17 rowsPerPage:10);
18
19 <div id="DivGrid">
20 @grid.GetHtml(
21 columns:grid.Columns(
22 grid.Column("StudentID", "Student ID"),
23 grid.Column("StudentName", "Student Name"),
24 grid.Column("StudentCode", "Student Code"),
25 grid.Column("Sex", "Sex"),
26 grid.Column("NRICPassport", "NRIC/Passport No.")
27 )
28 )
29 </div>
30 <h2>
31 Page Count:
32 @Html.Encode(grid.PageCount)
33 <br/>
34 Total Record:
35 @Html.Encode(grid.TotalRowCount)
36 </h2>
37 @Html.Encode(grid.FieldNamePrefix)
38 }