How to set a default value in Kendo DropDownList

I have a Kendo DropDownList, but, oddly enough, I cannot set its initial value.

Html.Kendo().DropDownList() .Name("PersonalCoachName") .BindTo(new SelectList((List<string>)ViewData["coachResources"])) .HtmlAttributes(new { style = "font-size:8pt;" }) 

ViewData ["coachResources"] is a list of row types. No matter what i use

 .BindTo(new SelectList((List<string>)ViewData["coachResources"], "Default")) or .SelectedIndex(3) 

DropDownList does not change its value and displays only the first value in the list. I need help with this. Thanks.

+7
source share
4 answers

Use the Value method. See the sample code below.

 @(Html.Kendo().DropDownList() .Name("DropDownListName") .DataTextField("Text") .DataValueField("Value") .BindTo(model.DropDownListItems) .Value(model.Selected) ) 

EDIT: DropDownList needs to be bound to List<SelectListItem> and it can be initialized as shown below.

 var items = new List<SelectListItem> { new SelectListItem { Text = "Item0", Value = "0" }, new SelectListItem { Text = "Item1", Value = "1" } }; 

In addition, I would recommend using MVVM to attach it to the view.

 public class DropDownViewModel { public String Selected; public List<SelectListItem> DropDownListItems; public DropDownViewModel(String selected, List<SelectListItem> dropDownListItems) { Selected = selected; DropDownListItems = dropDownListItems; } } 
+4
source

The value only works in jquery, not in the html helper. So it works in jQuery as

  var dropdownlist = $("#PersonalCoachName").data("kendoDropDownList"); dropdownlist.value(coachId); dropdownlist.refresh(); 
+6
source

Use the 'index' parameter when initializing the kendo combobox command to set the default value.

  $("#fabric").kendoComboBox({ autoWidth: true, dataTextField: "text", dataValueField: "value", dataSource: [ { text: "Cotton", value: "1" }, { text: "Polyester", value: "2" }, { text: "Cotton/Polyester", value: "3" }, { text: "Rib Knit", value: "4" } ], filter: "contains", suggest: true, index: 3 }); 
+1
source

There are two ways to set the default value when initializing the kendo drop-down list.

 $("#yourdropdownlist").kendoDropDownList({ dataTextField: "valueName", dataValueField: "valueCode", dataSource: [ { valueName: "A", valueCode: "0" }, { valueName: "B", valueCode: "1" } ], //Method 1 -set index //index: 0 //Method 2 - set default value and text value: "0", text:"θ‘Œζ”ΏεŒΊεˆ’" }); 
0
source

Source: https://habr.com/ru/post/1211976/


All Articles