Why does my MVCContrib Grid sorting code not work?

1.4k Views Asked by At

For some reason I cannot get my MVCContrib Grid sort to work.

Why does the following code results in a grid but without the sortable columns?

I'm using MVC3 and Razor syntax.

@model List<InboundShipmentItem>

@using MvcContrib.UI.Grid
@Html.Grid(Model).Sort((GridSortOptions)ViewBag.SortOptions).Columns(column =>
    {
        column.For(item => !item.Verified ? Ajax.ActionLink("Receive", "ReceiveItem", new {id = @item.Id},
                                                            new AjaxOptions
                                                                {
                                                                    HttpMethod = "GET",
                                                                    InsertionMode = InsertionMode.Replace,
                                                                    UpdateTargetId = "grid",
                                                                    OnSuccess = "InboundShipmentVerification.ReceiveItemSucceeded",
                                                                    OnFailure = "InboundShipmentVerification.ReceiveItemFailed"
                                                                }).ToString() : "Received").Named("Received?").Encode(false);
        column.For(item => item.PONumber).Named("PO#").Sortable(true);
        column.For(item => item.ShipQty).Named("Qty Sent").Sortable(true).SortColumnName("Qty");
        column.For(item => item.ReceivedQty).Named("Qty Recd");
        column.For(item => item.ISBN).Named("ISBN");
        column.For(item => item.Title).Named("Title");
        column.For(item => item.Author).Named("Author");
        column.For(item => item.InboundShipment.Status).Named("Shipment Status");
        column.For(item => item.InboundShipment.ShipmentId).Named("Shipment #");
    })
2

There are 2 best solutions below

0
kingdango On BEST ANSWER

The problem was that ViewBag.SortOptions was NULL. So if the GridSortOptions object is null the grid does not render any sort links for the header columns. To me this is silly behavior.

0
Can Ürek On
@Html.Grid(Model).Columns(column =>
{
////
}).Sort((GridSortOptions)ViewData["sort"])

You must fill that ViewData in your Controller.

Like that

public ActionResult Index(GridSortOptions sort, int? page)
{
   ///
   ///FILL YOUR MODEL 
   ///IEnumerable<YourModel> list = YourData.ToList() as IEnumerable<YourModel>;

    if (sort.Column == null)
    {
        sort.Column = "DefaultSortColumnName";
        sort.Direction = MvcContrib.Sorting.SortDirection.Ascending;
    }

    ViewData["sort"] = sort;

    return View(list);
}