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

DropDownList default value not passed to controller

2 Answers 1048 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 17 Aug 2018, 05:42 PM

We've got a drop down list inside a pop-up template from a grid. This drop down list is being populated correctly and you can select a value from the list and the value will be sent to the controller correctly. However when the data is loaded no value is selected initially so we are calling the select(0) function to automatically select the first item in the list. But when we do this the id value is not passed to the controller if the user doesn't select another item from the list.

Below is the DropDownList control. We are using the DataBound event to set the default value.

@(Html.Kendo().DropDownList()
                  .Name("ParentId")
                  .DataSource(ds => ds
                      .Custom()
                      .Transport(tr => tr
                          .Read(read => read.Action("OHGetGroupsForDropDown", "Groups")))
                      .Schema(sc => sc.Data("Data").Total("Total").Errors("errors")))
                  .DataValueField("Id")
                  .DataTextField("Name")
                  .Filter(FilterType.Contains)
                  .Events(e => e.DataBound("parentIdDefaults"))
                  .HtmlAttributes(new {@style = "width:100%", data_value_primitive = "true"}))

 

Below is the DataBound() event code. We are simply selecting the first item in the list.

function parentIdDefaults(e) {
        var dropdownlist = $("#ParentId").data("kendoDropDownList");
        dropdownlist.select(0);
    }

 

The above code works visually but if we do not select another item in the drop down list the ParentId value of the object passed to the controller is 0. If we select another item in the list the ParentId value passed to the controller is correct. 

Does anyone know why the select() call is not fully selecting the drop down list item but only appears to?

2 Answers, 1 is accepted

Sort by
0
Phillip
Top achievements
Rank 1
answered on 20 Aug 2018, 01:49 PM

So we figured this out in case anyone else runs across this.

We had to add the code to alert of the change after selecting the item as shown below.

function parentIdDefaults(e) {
        this.select(0);
        this.trigger('change');
    }
0
Dimitar
Telerik team
answered on 21 Aug 2018, 08:06 AM
Hello Phillip,

Thank you for sharing your solution.

You are indeed correct that the change event must be triggered manually through the trigger('change') method. This is the case, as the Grid uses observables internally for binding the popup fields, thus the select() method does not trigger the change event automatically. More details on this could be found in the API Documentation:


Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DropDownList
Asked by
Phillip
Top achievements
Rank 1
Answers by
Phillip
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or