2

我正在使用 Kendo UI Grid + DataSource,并且在与我的服务交互时遇到了一些问题:

我们有一个服务应该被调用如下:

 /find?criteria[0].FieldName=Name&criteria[0].Value=Test&criteria[1].FieldName=Description&criteria[1].Value=MyDescription

如何将这些参数从我的数据源传递给我的服务?

4

2 回答 2

4

你必须使用这样的parameterMap功能:

var data = kendo.data.DataSource({
    transport: {
        // these are passed to $.ajax()
        read: {
            url: "/find",
            dataType: 'json',
            type: 'GET',
            cache: false
        },
        update: {
            // ...
        },
        destroy: {
            // ...
        },
        create: {
            // ...
        },
        //
        parameterMap: function(options, type) {
            // edit VARS passed to service here!
            if (type === 'read') {
                return {
                    'criteria[0].FieldName': options.name,
                    'criteria[0].Value': options.value
                    // ...
                };
            }
        }
    },

    schema: {
        model: MyModel,
        type: 'json',

        parse: function(response) {
            // edit VARS coming from the server here!
            // ...
            return response;
        }
    }
});
于 2012-09-07T13:07:19.940 回答
1

如果您使用的是客户端 Json 绑定。检查:dataSource->transport->parameterMap 属性。

如:

$(function () {
    $("#grid").kendoGrid({
          .....
         dataSource: {
          ....
          transport: {
               parameterMap: function (data, operation) {
                    if (operation != "read") {
                        .....
                        return result;
                    } else {
                        //data sample: {"take":10,"skip":0,"page":1,"pageSize":10}
                        //alert(JSON.stringify(data)); //Need to insert custom parameters into data object here for read method routing. so, reconstruct this object.
                        data.CustomParameter1 = "@Model.Parameter1"; // Got value from MVC view model.
                        data.CustomParameter2 = "@Model.Parameter2"; // Got value from MVC view model.
                        return JSON.stringify(data); // Here using post. MVC controller action need "HttpPost"
                }
          }
     }
 }

MVC 控制器:

[HttpPost]
public ActionResult Read(int CustomParameter1, int CustomParameter2, int take, int skip, IEnumerable<Kendo.Mvc.Grid.CRUD.Models.Sort> sort, Kendo.Mvc.Grid.CRUD.Models.Filter filter)
{
         .....
}

公司项目样本在:

http://www.telerik.com/support/code-library/grid-bound-to-asp-net-mvc-action-methods---crud-operations

于 2014-02-20T19:06:11.810 回答