Reputation:
I'm using Kendo UI Grid + DataSource and I'm facing some issues interacting with my service:
We have a service which should be called as follows:
/find?criteria[0].FieldName=Name&criteria[0].Value=Test&criteria[1].FieldName=Description&criteria[1].Value=MyDescription
How I can pass the parameters as such to my service from my datasource?
Upvotes: 2
Views: 10539
Reputation: 1047
If you are using client side Json binding. check: dataSource->transport->parameterMap property.
such as:
$(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 controller:
[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)
{
.....
}
Corporated project sample in:
Upvotes: 1
Reputation: 18820
you have to use the parameterMap
function like this:
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;
}
}
});
Upvotes: 4