如何在Kendo DropDownList中设置默认值

用户585440

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

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

ViewData [“ coachResources”]是字符串类型的列表。不管我用

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

DropDownList不会更改其值,而只会显示列表中的第一个值。我需要这方面的帮助。谢谢。

好奇的超级英雄

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

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

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

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

另外,我建议使用MVVM将其附加到视图。

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章