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

DropDownList + DataSource Binding Help

8 Answers 1641 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Aleksander
Top achievements
Rank 1
Aleksander asked on 24 May 2012, 05:07 PM
I have a Drop Down list initialized as:
$('#ddlSearchPNResults').kendoDropDownList({
        dataTextField: "Text",
        dataValueField: "Value",
        autoBind: false
    });

Then in my Ajax call I have the following:
$.ajax({
            url: '/Home/SearchPerson',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: '{ "personName": "' + personName + '" }',
            success: function (retData) {
                if (JSON.stringify(retData) != "[]") {
                    var ddl = $('#ddlSearchPNResults').data("kendoDropDownList");
                    ddl.dataSource.data = retData;
                    ddl.refresh();

                    $('#divSearchPNResults').show();
                }
            },
            error: function (xhr, tStatus, err) {
                //alert(err);
            }
        });

But when I see the dropdownlist its still empty.

Please help.

8 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 28 May 2012, 09:37 AM
Have you tried using the setDataSource() method?
0
mo2011ti
Top achievements
Rank 1
answered on 25 Jul 2012, 03:35 PM
Is there any answer to this?
how to use setDataSource() method???? i cant find it

thanks
0
Chris
Top achievements
Rank 1
answered on 25 Jul 2012, 03:54 PM
Try changing your original code to this:

$.ajax({
            url: '/Home/SearchPerson',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: '{ "personName": "' + personName + '" }',
            success: function (retData) {
                if (JSON.stringify(retData) != "[]") {
                    var ddl = $('#ddlSearchPNResults').data("kendoDropDownList");
                    ddl.setDataSource(retData);
                    ddl.refresh();

                    $('#divSearchPNResults').show();
                }
            },
            error: function (xhr, tStatus, err) {
                //alert(err);
            }
        });
0
mo2011ti
Top achievements
Rank 1
answered on 25 Jul 2012, 03:57 PM
Chris
Thanks a lot!
That was very fast.
Moti
0
Chris
Top achievements
Rank 1
answered on 25 Jul 2012, 04:08 PM
No problem, hope it works!
0
mo2011ti
Top achievements
Rank 1
answered on 25 Jul 2012, 08:09 PM
works! THANKS
0
Chillax
Top achievements
Rank 1
answered on 30 Oct 2012, 08:06 AM
I am facing the same problem. My dropdown is not binding with the Ajax result. I am returning a List of my object. Here is the sample code.

Controller:
 public ActionResult SingleLevelOptions(int ParentID)
        {
            List<Models.Shared.SingleLevelOptions> OptionsList;
            Models.Shared.SingleLevelOptions obj = new Models.Shared.SingleLevelOptions();
            string username ="any user";
            OptionsList = obj.GetSingleLevelOptions(username, ParentID);
            return Json(new { OptionsList }, JsonRequestBehavior.AllowGet);
        }

View:
    <input id="ddlSearchPNResults" />

    <script type="text/javascript">
        $('#ddlSearchPNResults').kendoDropDownList({
            dataTextField: "OptionName",
            dataValueField: "OptionID",
            autoBind: false
        });
        $.ajax({
            url: '/Home/SingleLevelOptions',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: '{ "ParentID": "' + 53 + '" }',
            success: function (retData) {
                if (JSON.stringify(retData) != "[]") {
                    var ddl = $('#ddlSearchPNResults').data("kendoDropDownList");
                    ddl.setDataSource(retData);
                    ddl.refresh();
                    $('#divSearchPNResults').show();
                }
            },
            error: function (xhr, tStatus, err) {
                //alert(err);
            }
        });
    </script>
0
Paul
Top achievements
Rank 1
answered on 01 Nov 2012, 01:50 PM
HI All

This thread mentions the setDataSource as a method of DropDownList I cannot find it on the drop down list
my code:
var aspects = @Html.Raw(Model.Aspects.ToJson());
var l = $("#SourceAspectId").data("kendoDropDownList");
l.setDataSource(aspects);
l.refresh();

returns
TypeError: l.setDataSource is not a function
l.dataSource(aspects);
What might I be doing wrong? Ideally what I want to do is provide the data for the dropdown as embedded json rather than calling back to the server note I am attempting to use the MVC extensions rather than a script block to configure the drop down list.

Thanks
Tags
DropDownList
Asked by
Aleksander
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
mo2011ti
Top achievements
Rank 1
Chillax
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Share this question
or