【问题标题】:Binding data in created dropdown from ajax data在从 ajax 数据创建的下拉列表中绑定数据
【发布时间】:2014-06-10 05:38:01
【问题描述】:

我在 cshtml 页面中创建了以下下拉列表:

@(  Html.Kendo().DropDownList().Name("ddlCode").OptionLabel("Select Code...").BindTo(@ViewBag.dropDown)
            .DataTextField("Title")
            .DataValueField("domainCode")

我在检查我的页面上的一个复选框时绑定此下拉列表。

在检查复选框时,我调用了 javascript 函数并编写了 ajax 脚本,如下所示:

var ddl = $('#ddlCode').data("kendoDropDownList");
            $.ajax({
                url: "/PP/BindDropDown",
                data: {
                    'Id': paramID
                },
                dataType: "json",
                type: 'POST',
                cache: false,
                success: function (_data) {

                    ddl.dataSource.data(_data)

                },
                error: function () {
                    //
                }
            });

PPController 的BindDropdown 包含如下代码:

public JsonResult BindDropDown(string ID)
        {
            List<TEAMS_PP.Entity.correlations> list = new correlation().getDropDownvalues(ID);
            ViewBag.dropDown = list;
            return Json(list);
        }

我的问题是当下拉列表被绑定时,它的项目显示为“Undefined”,如下所示:

如何绑定这个下拉菜单???

我正在使用 MVC4 Kendo UI 控件

Entity.Correlations:

   public correlations() { }

    public correlations(DB.EH_PP_DmainComp item)
    {
        //this.code = Convert.ToInt32( Convert.ToString(item.domainCode));
        this.correlatedText = item.description;
        this.codeTitle = item.title;
        //Component 1a: Demonstrating Knowledge of Content and Pedagogy
        //ArrayList arrCode = new ArrayList();
        string[] arrCode = Convert.ToString(item.title).Split(':');

        string[] code = Convert.ToString(arrCode[0]).Split(' ');
        this.code = Convert.ToString(code[1]);

    }

    public DB.EH_PP_DmainComp ToDB()
    {
        var rec = new DB.EH_PP_DmainComp();

        return rec;
    }

    public DB.EH_PP_DmainComp ToDB(DB.EH_PP_DmainComp rec)
    {

        return rec;
    }
}

【问题讨论】:

  • TEAMS_PP.Entity.correlations 是什么?
  • 那只是一个分配属性的类
  • @Leo 我已经在我的问题中更新了它

标签: c# javascript .net ajax asp.net-mvc-4


【解决方案1】:

这里发生了什么……

@(Html.Kendo().DropDownList().Name("ddlCode").OptionLabel("Select Code...").BindTo(@ViewBag.dropDown)
        .DataTextField("Title")
        .DataValueField("domainCode")

您告诉DropDownListcorrelations 类中找到TitledomainCode 属性。但是,correlations 类没有这样的属性。

要完成这项工作,您必须执行以下操作之一:

  1. TitledomainCode 属性添加到correlations
  2. 使用公开此属性的不同模型对象,以便下拉列表可以绑定到它

【讨论】:

  • 谢谢,这是正确的解决方案:)
猜你喜欢
  • 2015-01-06
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多