简体   繁体   English

如何使用绑定到DataTable的Kendo Grid启用排序

[英]How to enable sorting with Kendo Grid bound to DataTable

I'm currently stuck when trying to implement the sorting feature of the Kendo Grid control. 我目前在尝试实现Kendo Grid控件的排序功能时陷入困境。 When I click a column to sort the values, it takes me to a 404 page. 当我单击列以对值进行排序时,它将带我到404页。 None of the documentation I've looked at is binding the grid to a DataTable and I'm also wondering if there needs to be a specific action for the sorting. 我看过的所有文档都没有将网格绑定到DataTable,而且我还想知道是否需要针对排序进行特定操作。 Can anyone help? 有人可以帮忙吗?

View Code: 查看代码:

    @{
        if (IsPost && Request.Url.AbsolutePath.Contains("Carriers"))
        {
                @(Html.Kendo().Grid((DataTable)(ViewData["CarrierResults"]))
                    .Name("carrierSearchResults")
                    .Sortable()
                )
        }
    }

Controller Code: 控制器代码:

    [HttpPost]
    public ActionResult Carriers()
    {
        DealerPortalRepository dpr = new DealerPortalRepository();
        // The SearchForCarriers method below returns a DataTable
        ViewData["CarrierResults"] = dpr.SearchForCarriers();  
        return View("~/Views/Search/Index.cshtml");
    }

Edit: Query code: 编辑:查询代码:

    public DataTable SearchForCarriers()
    {
        var query = from a in db.Affiliates
                    where a.AffiliateLevel == 1
                    select new { a.AffiliateName };
        return createCarrierTable(query);
    }

    public DataTable createCarrierTable(IEnumerable<dynamic> query)
    {
        // Create new DataTable since the query above does not produce a DataRow that follows a schema already defined in the database.
        DataTable dt = new DataTable();
        dt.Columns.Add(
            new DataColumn()
            {
                DataType = System.Type.GetType("System.String"),
                ColumnName = "Affiliate Name"
            }
        );

        // Add the row(s) to the DataTable.
        foreach (dynamic item in query)
        {
            var row = dt.NewRow();
            row["Affiliate Name"] = item.AffiliateName;
            dt.Rows.Add(row);
        }

        return dt;
    }

PS I know I should probably be using a model instead of ViewData, but I didn't originally implement it that way because the anonymous type in my DataTable was throwing me off. PS我知道我可能应该使用模型而不是ViewData,但是我最初并没有那样实现,因为DataTable中的匿名类型使我无法接受。

Since I cannot see the rest of your code, here is my guess. 由于我看不到您的其余代码,这是我的猜测。

Can you remove [HttpPost] and test it again? 您可以删除[HttpPost]并再次进行测试吗?

If still doesn't work, you need another action method which accepts DataSourceRequest as parameter. 如果仍然无法使用,则需要另一个操作方法,该方法接受DataSourceRequest作为参数。

public ActionResult Carriers_Read([DataSourceRequest]DataSourceRequest request)
{
  ...
}

Look at Grid Ajax Binding . 查看Grid Ajax绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM