简体   繁体   中英

Kendo Ajax Grid Parameters with .Net

I have a Kendo grid in which I am trying to send additional data as a bool which I can use to filter the return data in my controller.

Controller:

public ActionResult Securities_Read([DataSourceRequest]DataSourceRequest request, bool filter) {
    if (filter == true) // do something.
}

Read:

.Read(read => read.Action("Action", "Controller").Data("filterFunction"))

filter Function:

function filterFunction() {
    var filter = ($('#checkbox').is(":checked")) ? true : false;
    console.log(filter);
    return {
        filter: filter
    }
}

Checkbox event handler:

$('#checkbox').change(function () {
    $('#grid').data('kendoGrid').dataSource.read();
});

Any time the checkbox is changed it runs the change function which in turn calls the kendo grid to run the read function. The kendo grid also calls the filterFunction to get additional parameters. I can see that the filterFunction is indeed being called because a log the value of filter to the console. However once it gets back to the controller the value of the additional parameter is always null.

What am I missing?

It seem that filter is a key word use in the kendo framework. So when I changed:

function filterFunction() {
    var filter = ($('#checkbox').is(":checked")) ? true : false;
    console.log(filter);
    return {
        filter: filter
    }
}

to:

function filterFunction() {
    var filter = ($('#checkbox').is(":checked")) ? "true" : "false";
    console.log(filter);
    return {
        shouldFilter: filter
    }
}

Also notice that you can only pass strings as parameters so I had to change the parameter in the controller to a string and the true/false value to strings in the JavaScript function above:

public ActionResult Securities_Read([DataSourceRequest]DataSourceRequest request, bool filter) {
    if (filter == true) // do something.
}

to:

public ActionResult Securities_Read([DataSourceRequest]DataSourceRequest request, string filter) {
    if (filter == "true") // do something.
}

That solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM