0

In my static web method I populate a dictionary with the Bankid and Bankname that I select from a database. The Id goes to the Key and the Value goes to the value part. SO there're approximately 20 key-value-pairs in my dictionary. Then I return this dictionary to my ajax call:

      $.ajax({
            type: 'POST',
            url: 'AJAX.aspx/Banks',
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data)
            {
                $.each(data.d, function ()
                {
                    $("#testDiv").append(this.Key + " " +this.Value + "<br/>");
                });
            },
            error: function (x, e)
            {
                alert("The call to the server side failed. " + x.responseText);
            }
        });

In the above case I get 20 rows of undefined undefined. But if I remove both .Key and .Value from this I just get the value, so bank names. I need both the key and the value because I'm going to populate a select element with them-key for value and the value for inner html. In case you want to see my webmethod, here it is:

    [WebMethod]
public static Dictionary<string, string> Banks()
{
    Dictionary<string, string> dict = new Dictionary<string, string>();
    DatabaseProvider provider = GetDatabaseProvider();
    provider.AddOutParameter("V_CUR", OracleType.RefCursor);
    DataTable dt=provider.SelectDataTable("AGAPUS.PAYMENT.SP_BANKS", CommandType.StoredProcedure);
    foreach (DataRow row in dt.Rows)
    {
        dict.Add(row["PAYMENTTYPE"].ToString(), row["PAYMENTTYPEID"].ToString());
    }
    return dict;
}
4

1 回答 1

1

对我来说,这更像是一个关于 jQuery 的问题。也就是说,尝试将您的成功函数更改为:

success: function (data)
{
    $.each(data.d, function ( key, value )
    {
        $("#testDiv").append(key + " " + value + "<br/>");
    });
}

评估地图时,键和值作为参数传递给函数。

于 2012-11-19T14:39:10.340 回答