【问题标题】:How to loop through Json object to bind data to dropdownlist如何循环通过 Json 对象将数据绑定到下拉列表
【发布时间】:2017-09-20 07:10:38
【问题描述】:

我正在尝试遍历 Json 对象以在下拉列表中填充数据,但未定义。我不知道我在哪里做错了。下面是代码:

我的 Javascript 代码:

function CIOAnalyzeByDropDown() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            //url: '../../CIODashboard.svc/GetAnalyzedDropDownDetails',
            url: '../../CIODashboard/GetAnalyzedDropDownDetails',
            dataType: "json",
            processData: false,
            success: function (data) {
                debugger;
                //var s = JSON.parse(data.d);
                var s = JSON.stringify(data);
                debugger;
                for (x in s) {

                    if (s[x].dropDowns == "Created_Date") {
                        $("#drop1").append("<option value=" + s[x].dropDowns + "  selected >" + s[x].dropDowns + "</option>");
                    }
                    else {
                        $("#drop1").append("<option value=" + s[x].dropDowns + ">" + s[x].dropDowns + "</option>");
                    }
                }




            },
            error: function (result) {
                alert(result + "error occured while fetching the ProjectsSummary values");
            }
        });
    }

我的控制器代码:

 public string GetAnalyzedDropDownDetails()
    {
        string jsonString = string.Empty;
        List<projectDropDowns> itemList = new List<projectDropDowns>();

        using (SqlConnection con = new SqlConnection(strConnection))
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_AnalyzeByDropdown";
            cmd.CommandTimeout = 60;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtresult = new DataTable();
            da.Fill(dtresult);

            if (dtresult.Rows.Count > 0)
            {
                for (int i = 0; i < dtresult.Rows.Count; i++)
                {
                    projectDropDowns info = new projectDropDowns();
                    info.dropDowns = "" + dtresult.Rows[i]["COLUMN_NAME"];
                    itemList.Add(info);
                    jsonString = SerializeJSon<List<projectDropDowns>>(itemList);
                    //return jsonString;
                }
            }
            con.Close();
        }
        return jsonString;
    }

序列化Json方法:

public string SerializeJSon<T>(T t)
    {
        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
        ds.WriteObject(stream, t);
        string jsonString = Encoding.UTF8.GetString(stream.ToArray());
        stream.Close();
        return jsonString;
    }

它返回一个包含 3 个值的数组对象,但在我的下拉列表中显示未定义。 任何帮助将不胜感激。

【问题讨论】:

  • 如果你得到data = "[{"value":"359","text":"Karol Bagh"}]",那么你需要parse它就像var s = JSON.parse(data);而不是var s = JSON.parse(data.d);
  • jsonString = SerializeJSon&lt;List&lt;projectDropDowns&gt;&gt;(itemList); 需要移出你的 for 循环。我们还需要查看 projectDropDowns.cs 文件。

标签: jquery arrays json model-view-controller


【解决方案1】:

通过直接迭代数据而不将其解析为json来解决问题,因为传入的数据是一个数组。

 function CIOAnalyzeByDropDown() {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            //url: '../../CIODashboard.svc/GetAnalyzedDropDownDetails',
            url: '../../CIODashboard/GetAnalyzedDropDownDetails',
            dataType: "json",
            processData: false,
            success: function (data) {
                debugger;
                //var s = JSON.parse(data.d);
                //var s = JSON.stringify(data);

                for (x in data) {

                    if (data[x].dropDowns == "Created_Date") {
                        $("#drop1").append("<option value=" + data[x].dropDowns + "  selected >" + data[x].dropDowns + "</option>");
                    }
                    else {
                        $("#drop1").append("<option value=" + data[x].dropDowns + ">" + data[x].dropDowns + "</option>");
                    }
                }
            },
            error: function (result) {
                alert(result + "error occured while fetching the ProjectsSummary values");
            }
        });
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 2018-08-19
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多