开发者

ASP.net MVC3 Webgrid

开发者 https://www.devze.com 2023-04-05 06:14 出处:网络
I am using a webgrid to display some dynamic data. I have recently refactored my code, to move away from a hierarchal Model, containing various different types of data to be displayed in a View to usi

I am using a webgrid to display some dynamic data. I have recently refactored my code, to move away from a hierarchal Model, containing various different types of data to be displayed in a View to using ViewBag.

Previously the grid would sort the columns fine, just be clicking on the header, however sin开发者_StackOverflowce I changed to ViewBag, my table does not sort. My new code is as follows:

@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)
)
}

Anybody any ideas?

Thanks.


Make sure you have set the CanSort property to true on your ViewBag.columns:

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

Only columns that have this property set will appear as hyperlinks in the grid header allowing the user to sort on them.

0

精彩评论

暂无评论...
验证码 换一张
取 消