我正在尝试创建一个MVC WebGrid,我能够使网格正常工作,但面临排序问题。我将rowsPerPage
设置为5,共有7条记录。当我在第一页时,它显示前5个记录;当我对Id列进行排序时,它会对整组数据进行排序,并将第7条记录放在最前面。
我的问题:
代码是这样的: CSHTML -
@model IEnumerable<Product>
@{
ViewBag.Title = "grid";
WebGrid grid = new WebGrid(Model, rowsPerPage: 5 );
}
@grid.GetHtml(
tableStyle: "table",
fillEmptyRows: true,
headerStyle: "main-box-header clearfix",
footerStyle: "pagination pull-right",
mode: WebGridPagerModes.All, //paging to grid
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] // colums in grid
{
grid.Column("Id"), //the model fields to display
grid.Column("Name" ),
grid.Column("Description"),
grid.Column("Quantity"),
})
CONTROLLER -
public ActionResult WebgridSample()
{
List<Product> inventoryList = new List<Product>();
inventoryList.Add(new Product
{
Id = "P101",
Name = "Computer",
Description = "All type of computers",
Quantity = 800
});
inventoryList.Add(new Product
{
Id = "P102",
Name = "Laptop",
Description = "All models of Laptops",
Quantity = 500
});
inventoryList.Add(new Product
{
Id = "P103",
Name = "Camera",
Description = "Hd cameras",
Quantity = 300
});
inventoryList.Add(new Product
{
Id = "P104",
Name = "Mobile",
Description = "All Smartphones",
Quantity = 450
});
inventoryList.Add(new Product
{
Id = "P105",
Name = "Notepad",
Description = "All branded of notepads",
Quantity = 670
});
inventoryList.Add(new Product
{
Id = "P106",
Name = "Harddisk",
Description = "All type of Harddisk",
Quantity = 1200
});
inventoryList.Add(new Product
{
Id = "P107",
Name = "PenDrive",
Description = "All type of Pendrive",
Quantity = 370
});
return View(inventoryList);
}
答案 0 :(得分:0)
也许它不是真正的主题,但我会回答 实现这一目标的最有效方法是进行服务器端排序:
1)在控制器中你将有4个新参数
public ActionResult WebgridSample(int pageNum = 1, int pageSize = 5, string sortColumnName = "", string sortOrder = "desc")
2)在你发出SQL SELECT请求之后,你可以指出你需要从哪个位置检索条目并进行排序(sortOrder =&#34;&#34;取一个列表&#34;按原样#34;)
3)使用禁用autoSortAndPaging参数执行Bind()方法,应指向总条目(构造函数之前应该知道rowsPerPage)
4)并且在将所需数据加载到webgrid
之后不要忘记指向页码恭喜:)