2011-10-07 72 views
0

我怎樣才能實現這個網格我有Ajax?ASP.Net MVC 3.0 Razor查看網格Ajax分頁

@{ 
var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 5); 
} 
@if (Model.Count() > 0) 
{ 
<div id="grid"> 
    @grid.GetHtml(
tableStyle: "grid", 
headerStyle: "head", 
alternatingRowStyle: "alt", 
columns: grid.Columns(
grid.Column("FirstName", "First Name"), 
grid.Column("LastName", "Last Name"), 
grid.Column("Address"), 
grid.Column("DOB"), 
grid.Column("Gender") 
) 
    ) 
</div> 
} 

我試着添加ajaxUpdateContainerId,但它給出了一些錯誤。 我無法理解它。

我已經創建了我的格子像下面

var grid = new WebGrid(canPage: true, rowsPerPage: 2, defaultSort: "FirstName", canSort: true, ajaxUpdateContainerId: "grid"); 
grid.Bind(Model, rowCount:Model.Count(), autoSortAndPage: false); 
grid.Pager(WebGridPagerModes.All); 

但它顯示了所有的網格,我想只顯示5格記錄的記錄,然後分頁

回答

1

有一個偉大的文章這裏的主題http://www.unboxedsolutions.com/sean/archive/2011/01/23/15964.aspx

@using AdventureWorks.Common 
@using ThisController = MvcDemo.Controllers.GridExampleController 
@model GridExampleViewModel 

@{ ViewBag.Title = "Index"; } 

@{ 
    var grid = new WebGrid(canPage: true, rowsPerPage: ThisController.PageSize, canSort: true, ajaxUpdateContainerId: "grid"); 
    grid.Bind(Model.Employees, rowCount: Model.TotalRecords, autoSortAndPage: false); 
    grid.Pager(WebGridPagerModes.All); 
    @grid.GetHtml(htmlAttributes: new { id="grid" }, 
     columns: grid.Columns(
      grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { EmployeeID = item.EmployeeID, ContactID = item.ContactID })), 
      grid.Column("FullName"), 
      grid.Column("Title") 
     )); 
} 
+0

我創建網格這樣的VAR電網=新的WebGrid(canPage:真,rowsPerPage:2,defaultSort: 「名字」,canSort:TR ue,ajaxUpdateContainerId:「grid」); grid.Bind(Model,rowCount:Model.Count(),autoSortAndPage:false); grid.Pager(WebGridPagerModes.All); – HaBo

+0

看來你沒有在你的模型中使用skip和take方法。 queryResults.Skip(page * records).Take(records) –

+0

我做了一些像這樣grid.Bind(Model.Take(5),rowCount:Model.Count(),autoSortAndPage:false);它列出了50條記錄,當我點擊第二頁時,它仍然只顯示前5條記錄 – HaBo