模型绑定 KendoUI Grid 中的排序字段 [英] Model binding the sort field from KendoUI Grid
问题描述
我使用 KendoUI Grid 来显示数据.我的服务器分页工作非常迷人.剑道网格中的每一页变化都是对服务器的一个新的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.
这是来自 Kendo Grid 的请求的样子:
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
基本上它会说按 id 升序排序,然后按 reportName 升序排序".如何将这些数据放入我的操作方法中,而不必在 Request
中四处寻找并手动解析参数?
Basically it would be saying "sort by id in ascending order, then by reportName in ascending order". How can I get this data into my action method without having to poke around in Request
and manually parse the parameters?
推荐答案
ASP.NET MVC 模型绑定器不理解像 sort[0][field]
这样的表达式.它只理解 sort[0].field
这很不幸,因为 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:
- 将 Kendo UI Complete 用于 ASP.NET MVC.它带有一个用于网格请求的内置模型.可以在此处找到更多信息.
创建一个parameterMap并翻译排序表达式:
- 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;
}
<小时>
来自问题作者的更新:
我确实最终使用了参数映射,但我没有重新构造排序字段,而是简单地将选项字符串化并在 CRUD 传输上指定 contentType.只要指定了 contentType,模型绑定器就知道绑定到字符串化的 JSON.
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 Grid 中的排序字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!