Search code examples
asp.netasp.net-mvcasp.net-mvc-3razorwebgrid

webgrid sort not working


I'm using webgrid to show data from my data base. The problem is that sort option is not working Here is my webgrid code

@{
    var grid = new WebGrid(Model, canPage:true ,canSort:true, rowsPerPage :4);
    grid.Pager(WebGridPagerModes.NextPrevious);
        @grid.GetHtml(tableStyle: "webGrid", htmlAttributes: new { id = "datatable" },
        headerStyle: "webgrid-header",

        selectedRowStyle : "webgrid-selected-row",
        footerStyle : "webgrid-footer",
                 rowStyle: "webgrid-row-style",
                 alternatingRowStyle: "webgrid-alternating-row",
        columns: grid.Columns(
     grid.Column(header: "", format:  (item) =>

                    Html.ActionLink("Détails", "Details", new { id = item.id_client }, new { @class = "details" }), style: "actions"),

           grid.Column(header: "", format:  (item) => Html.ActionLink("Modifier", "Edit", new { id = item.id_client }, new { @class = "modifier" }),style : "actions"),

                    grid.Column(header: "", format: (item) => Html.ActionLink("Supprimer", "Delete", new { id = item.id_client }, new { @class = "supprimer" }), style: "actions"),

                    grid.Column("Nom",canSort: true),
                    grid.Column("Prenom", "Prénom",canSort:true),
        grid.Column("Email")));


                        }

Solution

  • You can try add the list of name columns for your webgrid, for example:

    var grid = new WebGrid(canPage:true ,canSort:true, rowsPerPage :4);
    
    grid.Bind(Model,columnNames: new[] { "Nom", "Prenom"}); //adding names of columns
    
    grid.Pager(WebGridPagerModes.NextPrevious);
    @grid.GetHtml(...
    

    Sometimes web grid can't understand how bind your model with columns.