c# - 从 ajax 数据创建的下拉列表中绑定(bind)数据

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

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

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

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

在检查复选框时,我调用了 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);
        }

我的问题是,当下拉菜单被绑定(bind)时,它会将其项目显示为“未定义”,如下所示:

enter image description here

如何绑定(bind)这个下拉菜单???

我正在使用 MVC4 Kendo UI 控件

实体.相关性:

   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;
    }
}

最佳答案

这里发生的事情是......

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

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

要实现此功能,您必须执行以下操作之一:

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

关于c# - 从 ajax 数据创建的下拉列表中绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24133622/

相关文章:

c# - C#中的静态方法、静态字段和多线程

c# - 用于记录方法调用和异常的 WCF 服务属性

Silverlight 中的 .Net 框架版本 : no List<T>. 查找方法?

c# - ASP.NET MVC URL 路由

c# - 使用 LINQ 创建字典并避免 "item with the same key has already been added"错误

javascript - 在 div 上的 anchor 标记内单击时停止单击事件传播

javascript - angular.js 简单函数

javascript - 重新载入谷歌地图

.net - Xamarin.Forms 和 .Net 标准 2.0

.net - 设置货币格式而不进行四舍五入