ASP.net MVC3 Webgrid

时间:2011-09-15 13:11:28

标签: c# asp.net asp.net-mvc-3 webgrid

我正在使用webgrid来显示一些动态数据。我最近重构了我的代码,从一个层级模型中移开,包含要在View中显示的各种不同类型的数据以使用ViewBag。

以前网格会对列进行排序,只需单击标题,但是由于我更改为ViewBag,因此我的表格不会排序。我的新代码如下:

@if (ViewBag.data != null)
{
var grid = new WebGrid(
    source: ViewBag.data,
    defaultSort: "StudyName",
    rowsPerPage: 10,
    canSort: true,
    canPage: true,
    ajaxUpdateContainerId: "tableDiv"
    ); 

@grid.GetHtml(
tableStyle: "resultTable",
columns: grid.Columns(
    ViewBag.columns)
)
}

有人有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:2)

确保您已在CanSort上将true媒体资源设为ViewBag.columns

ViewBag.columns = new[] 
{
    new WebGridColumn
    {
        ColumnName = "Id"
    },
    new WebGridColumn
    {
        CanSort = true,
        ColumnName = "StudyName"                
    },
};

只有具有此属性集的列才会在网格标题中显示为超链接,允许用户对其进行排序。

相关问题