KendoDropDownList默认选中值提交至Controller时为空的问题
The problem here is that while .select(1) correctly updates the UI selection, it doesn't always properly sync the underlying hidden input value that gets submitted with your form. Kendo DropDownList uses this hidden input to pass the selected value to your controller, so we need to ensure it's updated correctly.
Here are two reliable solutions to resolve this:
1. Set the value directly using the value() method
Instead of selecting by index, use the value() method to explicitly set the desired option's value. This ensures both the UI and the hidden submission input are properly synced:
function alternarDropDown(e) { var data = [ { text: "RESTRIÇÃO", value: "R" }, { text: "PENALIZAÇÃO", value: "P" } ]; var dropdown = $("#IndiceRestricaoPenalizacao"); var kendoDropdown = dropdown.kendoDropDownList({ dataSource: data, dataTextField: "text", dataValueField: "value", height: 100, change: onChange }).data("kendoDropDownList"); // Set the value explicitly instead of selecting by index kendoDropdown.value("P"); // Trigger the change event to run your dependent UI logic (like disabling the input) kendoDropdown.trigger("change"); }
2. Pre-set the value on the original element before initialization
You can set the default value of your target element before initializing the Kendo widget. This way, the DropDownList will automatically pick up and use this value:
function alternarDropDown(e) { var data = [ { text: "RESTRIÇÃO", value: "R" }, { text: "PENALIZAÇÃO", value: "P" } ]; var dropdown = $("#IndiceRestricaoPenalizacao"); // Set default value on the original element first dropdown.val("P"); dropdown.kendoDropDownList({ dataSource: data, dataTextField: "text", dataValueField: "value", height: 100, change: onChange }); // Your dependent input disabling logic if (dropdown.val() !== "R") { $("#VelocidadeRestricao").attr("disabled", "disabled"); } }
Why this works:
The select() method focuses solely on updating the UI selection, but doesn't always trigger the full internal state sync needed to update the hidden submission input. Using value() or pre-setting the original element's value ensures Kendo properly updates all parts of the widget, including the data that gets sent to your controller.
Also, remember that programmatic value changes don't automatically fire the change event—you need to trigger it explicitly if you rely on it to handle dependent UI updates like disabling other inputs.
内容的提问来源于stack exchange,提问作者Alessandro Macanha

