[英]How to enable sorting with Kendo Grid bound to DataTable
我目前在嘗試實現Kendo Grid控件的排序功能時陷入困境。 當我單擊列以對值進行排序時,它將帶我到404頁。 我看過的所有文檔都沒有將網格綁定到DataTable,而且我還想知道是否需要針對排序進行特定操作。 有人可以幫忙嗎?
查看代碼:
@{
if (IsPost && Request.Url.AbsolutePath.Contains("Carriers"))
{
@(Html.Kendo().Grid((DataTable)(ViewData["CarrierResults"]))
.Name("carrierSearchResults")
.Sortable()
)
}
}
控制器代碼:
[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");
}
編輯:查詢代碼:
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我知道我可能應該使用模型而不是ViewData,但是我最初並沒有那樣實現,因為DataTable中的匿名類型使我無法接受。
由於我看不到您的其余代碼,這是我的猜測。
您可以刪除[HttpPost]
並再次進行測試嗎?
如果仍然無法使用,則需要另一個操作方法,該方法接受DataSourceRequest作為參數。
public ActionResult Carriers_Read([DataSourceRequest]DataSourceRequest request)
{
...
}
查看Grid Ajax綁定 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.