【问题标题】:How to bind Kendo DropDownList to nested object如何将 Kendo DropDownList 绑定到嵌套对象
【发布时间】:2020-03-12 17:27:46
【问题描述】:

注意:这是使用 Kendo UI for .NET Core 包,虽然与 Jquery 版本类似,但请只关注这个平台。

最初,我有一个绑定到从 Controller 返回的 IEnumerable<Client> 的客户端下拉列表,它运行良好:

 @(Html.Kendo().DropDownList()
            .Name("ddlClient")
            .DataTextField("ClientName")
            .DataValueField("ClientID")
            .DataSource(source => {
                source.Read(read => {
                    read.Action("GetClient", "Home");
                });
            })           
        )

但在重组一些代码后,我希望能够从控制器返回一个IEnumerable<ClientViewModel>,而ClientViewModel 将是:

public class ClientViewModel{
   public Client SelectedClient {get; set;}
   public ClientDivison SelectedClientDivision {get; set;}
   public SomeOtherProperty TheSomeOtherProperty {get; set;}
   //etc
}

我将下拉列表修改为如下,但它没有按预期工作。我验证了 Controller 调用仍在触发并且仍在返回应有的记录(ClientViewModel 类型)。

这里是修改后的 DropDownList 代码

  @(Html.Kendo().DropDownList()
            .Name("ddlClient")
            .DataTextField("SelectedClient.ClientName")
            .DataValueField("SelectedClient.ClientID")
            .DataSource(source => {
                source.Read(read => {
                    read.Action("GetClient", "Home");
                });
            })           
        )

另外请注意,我不能将它绑定到为视图本身定义的主模型,因为它完全是一个不同的模型,这是一个独立的下拉菜单。

我觉得我以前引用过这样的嵌套属性。

我这里有什么问题?

【问题讨论】:

  • it is not working 是一个相当广泛的声明。它并没有告诉我们实际发生的事情与预期的相反。

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


【解决方案1】:

您似乎非常接近:请记住 MVC 默认将 JSON 对象键序列化为 camelCase

这对我有用:

  @(Html.Kendo().DropDownList()
            .Name("ddlClient")
            .DataTextField("selectedClient.clientName") // notice casing on both properties
            .DataValueField("selectedClient.clientID") // notice casing on both properties
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("GetClient", "Home");
                });
            })
            )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2017-08-30
    相关资源
    最近更新 更多