javascript - C# ListItemCollection to JSON, while keeping values and text items -


i have created web service (asmx file) returns serialized listitemcollection following code.

    public string getstates(string country)     {         listitemcollection lic = dbinterface.getstates(country);          var serialized = jsonconvert.serializeobject(lic);          return serialized;     } 

i call web service via javascript when user selects country dropdown list using following code.

            //ajax function uses web services states         function getstates(val)         {                               $.ajax({                 type: "post",                 url: "/webservices/getserverdata.asmx/getstates",                 data: json.stringify({country: val}),                 contenttype: "application/json; charset=utf-8",                 datatype: "json",                 success: function (data) {                     $("#ddlstate").empty();                     var parsed = json.parse(data.d);                     (var = 0; < parsed.length; i++) {                         $("#ddlstate").append("<option value='" + parsed[i] + "'>" + parsed[i] + "</option>");                     }                 },                 error: function (data) {                     alert(data.status + " " + data.statustext);                 }             });                             } 

the issue want keep not listitemcollection text, it's value. "jsonconvert.serializeobject returns text items. can return value , text can populate dropdown via javascript?

thanks!

one thing can use javascriptserializer() in system.web.script.serialization:

listitemcollection lic = new listitemcollection() {     new listitem("display text", "val1"),     new listitem("display text 2", "val2"), };  var ser = new javascriptserializer(); var serialized = ser.serialize(lic); 

results in (i took liberty format) :

[     {         "attributes": {             "keys": [],             "count": 0,             "cssstyle": {                 "keys": [],                 "count": 0,                 "value": null             }         },         "enabled": true,         "selected": false,         "text": "display text",         "value": "val1"     },     {         "attributes": {             "keys": [],             "count": 0,             "cssstyle": {                 "keys": [],                 "count": 0,                 "value": null             }         },         "enabled": true,         "selected": false,         "text": "display text 2",         "value": "val2"     } ] 

Comments