How can I accept JSON requests from a Kendo UI data source in my MVC4 application?

蹲街弑〆低调 提交于 2019-12-11 01:51:04

问题


I am using the Kendo UI grid in my MVC3 application and am quite pleased with it. I am using a Telerik provided example, excerpt below, to format the data posted by the grid's DataSource ally, and all is good. However, I don't want to have to rely on code like this. I would like to get Kendo and MVC talking without the 'translator', i.e. this code:

parameterMap: function(data, operation) {
    var result = { };
    for (var i = 0; i < data.models.length; i++) {
        var model = data.models[i];
        for (var member in model) {
            result["models[" + i + "]." + member] = model[member];
        }
    }
    return result;
}

This function is a 'hook' that allows me to manipulated data before Kendo ajaxes it out. By default, the Kendo DataSource sends content-type form-encoded, but not quite right for the MVC model binder. Without this, I can still use a FormCollection and do my own binding, but that is not on.

When I configure the DataSource to send JSON, and change my mapping function to look like this

parameterMap: function(data, operation) {
    return JSON.stringify(data);
}

I get the following data being send in the request, but now I have no idea how to get MVC to bind to this. Right now my only hope is to grab Request.Params[0] in the action method, and deserialize this JSON myself.

I don't think I should have to write any code to get two HTTP endpoints to communicate properly using JSON in this day and age. What am I doing wrong, or, what should I be looking at on my side, i.e. the receiver of the requests. I would really prefer to minimize my intervention on the client side to maybe just the stringify call.


回答1:


No idea if this is still a problem or not since this is a rather old question, but I had a scenario where I was shipping up json data to my controller and I had to give it a "hint" on what the name was so that model binding would work correctly:

public JsonResult GetDatByIds([Bind(Prefix="idList[]")]List<Guid> idList)

In my scenario, kendo was serializing my data and giving it a name of idList[] in the form post rather than just idList. Once I gave it the model binding hint, it worked like a charm. This might be the same for your scenario.



来源:https://stackoverflow.com/questions/10240422/how-can-i-accept-json-requests-from-a-kendo-ui-data-source-in-my-mvc4-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!