从KendoUI网格模型绑定排序字段 [英] Model binding the sort field from KendoUI Grid
问题描述
我用KendoUI网显示的数据。我有服务器的分页像一个魅力的工作。在剑道网格中每个页面的变化是一个新的AJAX请求到服务器和服务器返回的数据的正确的页面。我现在试图做服务器端的排序,但我遇到了麻烦模型绑定绑定到排序值。
I'm using KendoUI Grid to show data. I have server paging working like a charm. The each page change in the kendo grid is a new ajax request to the server and the server returns the correct page of data. I am now trying to do server-side sorting, but I'm having trouble getting model binding to bind to the sort values.
这是从剑道电网的要求是这样的:
This is what the request from the Kendo Grid looks like:
我的操作方法是这样的:
My action method looks like this:
public JsonResult GetReports(int pageSize, int skip, List<KendoSort> sort)
{
// sort is not being populated with the right data.
}
KendoSort是一个自定义类:
KendoSort is a custom class:
public class KendoSort
{
public string Field { get; set; }
public string Dir { get; set; }
}
我知道我不是这样做的权利。如何使我的操作方法寻找正确捕捉排序提供的数据?截屏显示在排序集合只有一个项目,但电网可以传递更多。例如,它也可以包含一个额外的排序:
I know I'm not doing this right. How should my action method look to correctly capture the data supplied for the sort? The screenshot shows only a single item in the sort collection, but the grid could pass more. For example, it could also have included an additional sort:
sort[1][field]: reportName
sort[1][dir]: asc
基本上,它会说排序编号升序排列,然后所以reportName升序排列。我怎样才能得到这些数据转化为我的行为方式,而不必闲逛在要求
和手动解析参数?
推荐答案
在ASP.NET MVC模型绑定不明白EX pressions像排序[0] [现场]
。据了解仅排序[0]点域
这是不幸的,因为 jQuery.ajax
提交前格式嵌套的对象。
The ASP.NET MVC model binder does not understand expressions like sort[0][field]
. It understands only sort[0].field
which is unfortunate because jQuery.ajax
submits nested objects in the former format.
有两种方法来解决这个问题:
There are two ways to solve the problem:
- 使用剑道UI完整的ASP.NET MVC。它配备了一个内置的模式,为电网的要求。更多信息可以发现<一href="http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding">here.
-
创建一个<一个href="http://docs.kendoui.com/api/framework/datasource#transportparametermap-function">parameterMap并翻译排序前pression:
- Use Kendo UI Complete for ASP.NET MVC. It comes with a built-in model for the grid request. More info can be found here.
Create a parameterMap and translate the sort expression:
parameterMap: function(options) {
var result = {
pageSize: options.pageSize,
skip: options.skip
};
if (options.sort) {
for (var i = 0; i < options.sort.length; i++) {
result["sort[" + i + "].field"] = options.sort[i].field;
result["sort[" + i + "].dir"] = options.sort[i].dir;
}
}
return result;
}
我没有最终使用参数映射,而不是重新结构中的排序字段我只是字符串化的选项,指定的contentType的CRUD传输。该模型粘合剂知道要绑定到字符串化的JSON只要指定了的contentType。
UPDATE FROM QUESTION AUTHOR:
I did end up using parameter map, but rather than re-structure the sort field I simply stringified the options and specified the contentType on the CRUD transports. The model binder knows to bind to the stringified JSON as long as the contentType is specified.
transport: {
read: {
url: '...',
type: 'POST',
contentType: 'application/json'
},
update: {
url: '...',
type: 'POST',
contentType: 'application/json'
},
destroy: {
url: '...',
type: 'POST',
contentType: 'application/json'
},
create: {
url: '...',
type: 'POST',
contentType: 'application/json'
},
parameterMap: function (options, type) {
return JSON.stringify(options);
}
}
这篇关于从KendoUI网格模型绑定排序字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!