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

Resetting datasource leaves Id as Text in combobox

2 Answers 347 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
TAD RHODES
Top achievements
Rank 1
TAD RHODES asked on 06 Feb 2013, 03:13 PM
I have some odd behavior happening in a combo box.

I have a simple combo box setup.

@(Html.Kendo().ComboBox()
                  .Name("vendorsforClass")
                  .AutoBind(false)
                  .HtmlAttributes(new { style = "width: 300px;" })
                  .DataTextField("Name")
                  .DataValueField("Id")
                  )
I have a link button that calls this javascript code.

function getVendorsForClass() {
 
        var txtValue = $("#txtSearchClassVendor").val();
 
        $.getJSON('@Url.Action("GetVendorListByClass","RCS", new {classes = "class"}, "http")/' + txtValue, function (data) {
 
            var combo = $("#vendorsforClass").data("kendoComboBox");
            combo.setDataSource(data);
            combo.refresh();
        });
}
This is the code in the controller that is creating the json object.

public JsonResult GetVendorListByClass(string classes)
        {
            var vendors = new List<VendorList>();
 
            var classId = classes.Split('/')[1];
 
            int outId;
            if (int.TryParse(classId, out outId))
            {
                vendors = new VendorListFactory().GetVendors(outId);
            }
 
            return this.Json(vendors.Select(v => new { v.Name, v.Id }), JsonRequestBehavior.AllowGet);
        }
Everything works perfectly on the first time I click on the link to populate the combo box.  If I select a value in the combo box, say for example it's Id happens to be 1234, when I click on the link again (with the intention of repopulating the combo box with different set of values) with a different value that returns back an empty object (no results in the list), the combo box is left with no values in the drop down (which would be correct), but the text in the combo box say 1234.

The box is left with the Id on the previous value I selected.  I thought when I call setDataSource it rebinds the combo box, meaning it removes previously selected values.  Why is the combo box setting it's text as the previous value (Id) that I had selected when the second datasource happens to be an empty list?

Thanks,
Tad

2 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 08 Feb 2013, 09:56 AM
Hi Tad,

Thank you for contacting us.

The value that you see is actually the value of the ComboBox' input element which does not reset automatically. The solution would be to reset it manually after rebind. Please try the following:
combo.setDataSource(data);
combo.value(""); //clear the value

Refresh method have other purpose - to re-render the items of the widget's drop-down list.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
TAD RHODES
Top achievements
Rank 1
answered on 08 Feb 2013, 02:12 PM
Excellent...thank you.
Tags
ComboBox
Asked by
TAD RHODES
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
TAD RHODES
Top achievements
Rank 1
Share this question
or