Hi,
I'm trying to understand the data binding for a combo box control.
Approach 1. - Using the bind-to attribute. I added "Selected=true" to one of item in the continents data source. This is okay the control pre-selects the item correctly.
@{
var continents = new List<SelectListItem> {
new SelectListItem() {Text = "Africa", Value = "1"},
new SelectListItem() {Text = "Europe", Value = "2"},
new SelectListItem() {Text = "Asia", Value = "3"},
new SelectListItem() {Text = "North America", Value = "4", Selected=true},
new SelectListItem() {Text = "South America", Value = "5"},
new SelectListItem() {Text = "Antarctica", Value = "6"},
new SelectListItem() {Text = "Australia", Value = "7"}
};
}
<h4>ComboBox:</h4>
<kendo-combobox name="combobox1"
datatextfield="Text"
datavaluefield="Value"
placeholder="Select a value"
bind-to="continents"
style="width: 250px;">
</kendo-combobox>
Approach 2. - Ajax binding. I have the same datasource as Approach 1 to return in TestController.cs. I"m expecting "North America" should be pre-selected but it's not. Is this an expected behavior by design when using Ajax binding that "Selected=true" is not appliable? The reason why I'd like to use "Selected=true" with Ajax binding is because there are some logics in a controller action that set the default value for a combo box. I don't want to use the "value" attribute. I knew using the value attribute works for sure.
TestController.cs
public IActionResult Getcontinents(string payload)
{
var continents = new List<SelectListItem> {
new SelectListItem() {Text = "Africa", Value = "1"},
new SelectListItem() {Text = "Europe", Value = "2"},
new SelectListItem() {Text = "Asia", Value = "3"},
new SelectListItem() {Text = "North America", Value = "4", Selected=true},
new SelectListItem() {Text = "South America", Value = "5"},
new SelectListItem() {Text = "Antarctica", Value = "6"},
new SelectListItem() {Text = "Australia", Value = "7"}
};
return Json(continents);
}
Index.cshtml
<kendo-combobox name="combobox2"
datatextfield="Text"
datavaluefield="Value"
filter="FilterType.Contains">
<datasource server-filtering="false">
<transport>
<read url="/Test/Getcontinents"/>
</transport>
</datasource>
</kendo-combobox>
Any insights or thoughts?
Thanks,
Bob D