为什么我的MVCContrib网格排序代码不起作用?

时间:2012-08-23 15:28:05

标签: asp.net-mvc-3 razor mvccontrib-grid

出于某种原因,我不能让我的MVCContrib Grid排序工作。

为什么以下代码会产生网格但没有可排序的列?

我正在使用MVC3和Razor语法。

@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 个答案:

答案 0 :(得分:4)

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

您必须在Controller中填充该ViewData。

喜欢那个

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

答案 1 :(得分:3)

问题是 ViewBag.SortOptions为NULL 。因此,如果GridSortOptions对象为null,则网格不会为标题列呈现任何排序链接。对我来说这是愚蠢的行为。