0

In my asp.net web page, I have prepared the JSon string with the help of JavaScriptSerializer class. And i have spit the Json string in to the HTML markup with the help of RegisterStartupScript method and it looks like this,

C#.net code to prepare the json string,

System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

List<Dictionary<string, string>> glAccts = new List<Dictionary<string, string>>();
Dictionary<string, string> row;

foreach (DataRow dr in _dtProfitCenterRawData.Rows)
{
    row = new Dictionary<string, string>();
    row.Add(dr[0].ToString(), dr[1].ToString());
    glAccts.Add(row);
}

string jsonObj = jsSerializer.Serialize(glAccts);
string script = "var glList = " + jsonObj + ";";

ClientScriptManager cs = Page.ClientScript; 
cs.RegisterStartupScript(Page.GetType(), "JSONObj", script, true);

The glList variable on the client html looks like this,

var glList = [
    { "1110005": "1110005 - X1" },
    { "1110008": "1110008 - X2" },
    { "1110011": "1110011 - X3" },
    { "1110020": "1110020 - X4" }
];

I want to bind this json string to dropdownlist control. Please suggest how to perform that? I tried to perform the following action to view the data inside the object, but it does not give me the real values. It gives [object] in the alert methods.

Please suggest the fix to the problem..

$.each(glList, function (val, text) {
    //$('#mySelect').append(new Option(text, val));
    alert(text); alert(val);                 
});
4
  • possible duplicate of What is the best way to add options to a select from an array with jQuery? Commented Jun 19, 2014 at 9:49
  • Yeh, i tried but it is not working for me.. Do i need to remove the [, ] brackets from the variable glList ?? Commented Jun 19, 2014 at 9:51
  • The only difference between their JSON data and yours is they have a object of {"key1":"value", "key2":"value", "key3":"value"}, whereas you have a list of [{"key":"value"},{"key":"value"},{"key":"value"}] Commented Jun 19, 2014 at 9:54
  • Any idea, how do i prepare the json style data from c#.net.. Can i fix the problem in my c# code.. Commented Jun 19, 2014 at 9:57

3 Answers 3

2

Try this here. If you itereate through glList you get the objects but not their properties.

function jsonTest() {
    $.each(glList, function(index, obj) {
        ($.each(obj, function (key, value) {
            $('#mySelect').append(new Option(key, value));
        }));
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use JSON.Net instead JavaScriptSerializer to serialize a Dictionnary.

Or you can try to cast to object before serialize

jsSerializer.Serialize((object)glAccts)

1 Comment

I tried this , but there is no difference in the output. I cant use JSon.NET library at this moment now..
0

try

 var glList = [
        { 1110005: "1110005 - X1" },
        { 1110008: "1110008 - X2" },
        { 1110011: "1110011 - X3" },
        { 1110020: "1110020 - X4" }
    ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.