This is a migrated thread and some comments may be shown as answers.

SIMPLE example refuses to work. Bind JSON result using datasource

2 Answers 337 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 24 Jul 2012, 09:55 PM
Maybe i am just old.. but i have worked on this thing all day with no love.

This is my latest:

$(document).ready(function () {
            var selectedRegionId;
            var dsCategories;

            $("#selectCategory").width(180).kendoComboBox({
                dataTextField: "CostDriverCategoryName",
                dataValueField: "CostDriverCategoryId",
                dataSource: dsCategories
            });


            dsCategories = new kendo.data.DataSource({
                transport: {
                    read: "/CostDriver/GetCostDriverCategoriesByRegion/" + $("input[name='region']").val(),
                    data: $("input[name='region']").val()
                }
            });

            $("#selectCommodity").width(180).kendoComboBox({ enabled: false });

            $("input[name='region']").click(function () {
                // alert(selectedRegionId);
                dsCategories.read();
            });
        });



I have a few issues:

1. HOW do you make the "data" parameter in the transport read work for a GET call to an ASP.Net MVC JsonResult controller method? I have searched the docs and these forums. I see people avoiding the wrapper and calling using jquery Ajax directly.. but seriously,, this datasource is a wrapper for Jquery ajax calls.. does it not work????

2. My result is a JSON object that looks like:

[{"CostDriverCategoryId":2,"CostDriverCategoryName":"Chemicals","ShortName":null},{"CostDriverCategoryId":3,"CostDriverCategoryName":"Currency","ShortName":null}
]

What do i need to do to have this result actually bind to the combobox as i defined in the combobox itself? I can get the result from the read by passing the data value right in the URL itself. Fine.. But it does not bind to the combobox.

Thanks.

2 Answers, 1 is accepted

Sort by
0
Edemilson
Top achievements
Rank 1
answered on 02 Sep 2012, 12:22 AM
I did try every example here and at the documentation, but ComboBox treats my dataSource as a string instead of a JSON, so it displays every character as an option in the combo list, and sometimes it display "undefined"... The only thing that worked for me was:

$.getJSON('/get/items', function(data) {
    $('#comboBox').kendoComboBox({
        dataSource: data
    });
});

At my application side I have:

// code to generate the array as a simple list like:
// $result = array('Item 1', 'Item 2', 'Item 3', ..., 'Item N');
header('Content-Type: application/json');
echo json_encode($result);

The example below (taken from the manual) should work, but it doesn't... :(

$("#comboBox").kendoComboBox({
    dataSource: new kendo.data.DataSource({
        transport: {
            read: {
                url: "Get/Items" // url to remote data source (simple list of strings)
            }
        }
    });
});
0
Nohinn
Top achievements
Rank 1
answered on 07 Sep 2012, 11:33 AM
Rich, in your case unless you define the read action of the datasource to use the POST method, it uses GET.
In your controller action in your jsonresult you should set the JsonRequestBehavior to AllowGet.

In your case Edemilson, use the last example, the one from the documentation and try to use "print json_encode($result);" instead of echo
Tags
ComboBox
Asked by
Rich
Top achievements
Rank 1
Answers by
Edemilson
Top achievements
Rank 1
Nohinn
Top achievements
Rank 1
Share this question
or