如何在Kendo DropDownList中设置默认值

use*_*440 7 kendo-ui kendo-dropdown

我有一个Kendo DropDownList,但奇怪的是我无法设置它的初始值.

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

ViewData ["coachResources"]是一个字符串类型的列表.无论我使用

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

DropDownList不会更改它的值,只显示列表中的第一个值.我需要帮助.谢谢.

use*_*440 6

该值仅适用于jquery,而不适用于html帮助程序.所以它适用于Jquery

    var dropdownlist = $("#PersonalCoachName").data("kendoDropDownList");
    dropdownlist.value(coachId);
    dropdownlist.refresh(); 
Run Code Online (Sandbox Code Playgroud)


Cur*_*ero 5

使用价值方法。请参阅下面的示例代码。

@(Html.Kendo().DropDownList()
      .Name("DropDownListName")
      .DataTextField("Text")
      .DataValueField("Value")
      .BindTo(model.DropDownListItems)
      .Value(model.Selected)
      )
Run Code Online (Sandbox Code Playgroud)

编辑: DropDownList 需要绑定到List<SelectListItem>并且可以初始化,如下所示。

var items = new List<SelectListItem>
{
    new SelectListItem { Text = "Item0", Value = "0" }, 
    new SelectListItem { Text = "Item1", Value = "1" } 
};
Run Code Online (Sandbox Code Playgroud)

另外,我建议使用MVVM来附加它来查看。

public class DropDownViewModel
{
    public String Selected;
    public List<SelectListItem> DropDownListItems;

    public DropDownViewModel(String selected, List<SelectListItem> dropDownListItems)
    {
        Selected = selected;
        DropDownListItems = dropDownListItems;
    }
}
Run Code Online (Sandbox Code Playgroud)