【问题标题】:How do you bind a kendo dropdownlist to a model property?如何将剑道下拉列表绑定到模型属性?
【发布时间】:2018-07-17 16:40:33
【问题描述】:

我的视图中定义了一个剑道下拉列表,如下所示:

        @(Html.Kendo().DropDownList()
                .Name("RowCategoryID")
                .OptionLabel("Select Row Category...")
                .BindTo(new SelectList(Model.RowCategories.Select(r => new { Key = r.Key, Value = r.Value }), "Key", "Value"))
                .HtmlAttributes(new { style = "width: 50%" })

我的模型有一个名为 RowCategoriesDictionary<int,string>,其中包含 DDL 的键/值对。它还有一个名为RowCategoryIDint 属性,表示从RowCategories 列表中选择的当前RowCategory

DDL 使用来自RowCategories 的值进行填充,但它不会选择值为RowCategoryID 的选项。它只是默认为“选择行类别...”。

如何让它选择值为RowCategoryID的选项?

【问题讨论】:

    标签: asp.net-mvc kendo-ui kendo-asp.net-mvc


    【解决方案1】:

    尝试使用Value()的方法:

    @(Html.Kendo().DropDownList()
                .Name("RowCategoryID")
                .OptionLabel("Select Row Category...")
                .BindTo(new SelectList(Model.RowCategories.Select(r => new { Key = r.Key, Value = r.Value }), "Key", "Value"))
                .Value(Model.RowCategoryID) // <- this
                .HtmlAttributes(new { style = "width: 50%" })
    

    虽然我在the documentation 中找不到该方法,但我发现了demos(参见“基本用法”)。

    【讨论】:

    • 很遗憾,没用。我注意到一个模式。 .Value 需要 string,而 RowCategoryIDint。任何 Dictionary 类型都不能正确绑定,但 Dictionary 可以。想知道是否有联系。
    • @Legion 好,然后尝试投射它Value(Model.RowCategoryID.ToString())。你试过吗?另外,您不需要更改您的字典,只需 Key = r.Key.ToString()
    • 想通了。原来它与剑道无关。 RowCategoryID 已在服务器端填充,但在 javascript 中丢失,使控件无法绑定。
    • @Legion 很高兴听到!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2017-02-06
    相关资源
    最近更新 更多