Sharon
Sharon

Reputation: 767

Populate items in dropdownlist

I have one function in my code behind

    [System.Web.Services.WebMethod]
    public static pcpName[] getPcpNames(string pcpCounty, string claimType)
    {
        List<pcpName> pcpNames = new List<pcpName>();
        string query = "SELECT DISTINCT [PCP_ID], [PCP_NAME]+':'+[PCP_ID] AS [PCP_NAME] FROM [dbo].[FreedomTrinity] WHERE [PCP_COUNTY] = '" + pcpCounty + "' AND [CLAIM_TYPE] = '" + claimType + "'";

        SqlDataReader reader = Database.SQLRead(query);
        while (reader.Read())
        {
            pcpName names = new pcpName();
            names.PCP_ID = reader.GetString(0);
            names.PCP_NAME = reader.GetString(1);
            pcpNames.Add(names);
        }

        return pcpNames.ToArray();
    }

Now I want to populate items in a drop down list using this out put using jQuery. So I write the code like this in my js file.

 $(document).ready(function () {
$("#drpPcpCounty").change(function () {
        //Remove items from drpPcpName
        $("#drpPcpName option").remove();

        $.ajax({
            type: "POST",
            url: "FreedomContestation.aspx/getPcpNames",
            data: '{pcpCounty: "' + $("#drpPcpCounty").val() + '", claimType: "' + $("input:radio[name='rbtnlstClaimType']:checked").val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                response($.map(data.d, function (item) {                      
                for (i in data) {
                    var d = data[i];
                $('#drpPcpName').append($("<option></option>").attr("value", d.PCP_ID).text(d.PCP_NAME));
                }
                }))
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    });
});

But nothing is happening in dropdown list. Code behind code is returning the array with values. What to do after success: ??

EDIT 1

I track the code till response($.map(data.d, function (item) { . But I don't know what's happening inside it. No alert() working inside response($.map(data.d, function (item) {

Upvotes: 1

Views: 109

Answers (1)

Anton Levshunov
Anton Levshunov

Reputation: 408

Try this:

success: function (data) {
    for (var i = 0;i < data.d.length;i++) {
            var d = data.d[i];
            $('#drpPcpName').append($("<option></option>").attr("value", d.PCP_ID).text(d.PCP_NAME));
        }
},

Upvotes: 1

Related Questions