1

I am getting this result as JSON output. How do I bind this output in my dropdown using jquery ?

This is jquery code:

function OnSuccess(response) {  

     var ddlCity = $("[id*=SiteMaster_ddlCity]");
     var ddlState = $("[id*=SiteMaster_ddlState]");
     ddlCity.empty().append('<option selected="selected" value="0">Please select</option>');
     ddlState.empty().append('<option selected="selected" value="0">Please select</option>');                            
     $.each(response.d, function () {
           ddlCity.append($("<option></option>").val(this['CITY']).html(this['CITY']));
                ddlState.append($("<option></option>").val(this['STATE']).html(this['STATE']));
            });

     //  alert(response.d);
} 

This is the code returning the JSON output

return JsonConvert.SerializeObject(dsCityStatebyZipCode, Formatting.Indented);
2
  • u could use the response value, $.each function pass the value and index of the enumerated variable; in short u should fill in the params at $.each function () <- in between this parenthesises Commented Sep 4, 2017 at 6:44
  • Hi thanks :) It worked Commented Sep 4, 2017 at 7:46

1 Answer 1

3

you need to apply the index and value in each loop to get the proper value here is the correct syntax to use it

$('.dropdown-content').html('');  //html element where you need to bind the data
    var parseData = jQuery.parseJSON(result.d); // parse the data
    var html = '';            
    $.each(parseData, function (i, v) {   // get data using each loop
        html += "<option>" + v.name+ "</option>";
    });

    $('.dropdown-content').html(html);  // bind the html to targeted element
Sign up to request clarification or add additional context in comments.

8 Comments

It is binding as undefined in Dropdownlist :(
use the debugger and see what is your response
var parseData = jQuery.parseJSON(response.d); // parse the data+ var html = ''; $.each(parseData, function (i, v) { html += "<option>" + v.CITY+ "</option>"; });
show me your data format in parseData if there is city in data then they definitely bind the city debug it and show me your data format under parseData variable
parseData = {Table: Array(1)}, response = {d: "{ ↵ "Table": [ ↵ { ↵ "CITY": "DPO", ↵ "STATE": "Armed Forces America" ↵ } ↵ ] ↵}"} I am getting parsedata like this in Debugger
|

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.