Sorting by date in Webgrid MVC

Total Post:10

Points:70
 429  View(s)
Ratings:
Rate this:

I want to show record by date wise what is the code to do sorting by date

@{

    var grid = new WebGrid(canSort: true, canPage: true, rowsPerPage: 10);
    grid.Bind(Model, autoSortAndPage: false, rowCount: Convert.ToInt32(ViewBag.TotalPost));
    grid.Pager(WebGridPagerModes.All);

}
    <div>
        @grid.Table(tableStyle: "table table-striped",
                                      columns: grid.Columns(
                                     grid.Column("Title", format: @<text>@item.Title</text>),
                                       grid.Column("Date", format: @<text>@item.Date</text>),
                                    )) 
</div>


  1. Post:5

    Points:35
    Re: Sorting by date in Webgrid MVC

    just write follwoing code

    @{
    
        var grid = new WebGrid(canSort: true, canPage: true, rowsPerPage: 10);
        grid.Bind(Model.OrderByDescending(x=>x.Date), autoSortAndPage: false, rowCount: Convert.ToInt32(ViewBag.TotalPost));
        grid.Pager(WebGridPagerModes.All);

    hope it resolve your problem

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!