如何循环遍历Json对象以将数据绑定到dropdownlist

时间:2017-09-20 07:10:38

标签: jquery arrays json model-view-controller

我试图循环一个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;
    }

Serialize 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个值的数组对象,但在我的下拉列表中显示未定义。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题通过直接迭代数据并且不将其解析为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");
            }
        });
    }