开发者

Convert Keys & Values in Dictionary<int, string> to a String[] Array

I have a webservice function that returns something like the following:

  • New Orleans, Louisiana
  • New York, New York
  • Newark, New Jersey

This data comes from the following function:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetCitiesWithState(string isoalpha2, string prefixText)
{
    var dict = AtomicCore.CityObject.GetCitiesInCountryWithStateAutocomplete(isoalpha2, prefixText);
    string[] cities = dict.Values.ToArray();
    return cities;
}

It works wonderfully, but what I really need is a list like this:

  • 12, New Orleans, Louisiana
  • 22, New York, New York
  • 48, Newark, New Jersey

With the # being the City Id (this is contained with dict, which is type of: Dictionary<int, string>).

The reason I am doing this is because I have some Jquery that reads an ASMX service and this method, and I need to be able to see the City Id of the selected city. This is my Jquery for clarity (which works currently):

$('#<%=txtCity.ClientID%>').autocomplete({
            source: func开发者_运维技巧tion (request, response) {
                var parameters = {
                    isoalpha2: '<%=Session["BusinessCountry"].ToString()%>',
                    prefixText: request.term
                };
                $.ajax({
                    url: '<%=ResolveUrl("~/AtomicService/Assets.asmx/GetCitiesWithState")%>',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify(parameters),
                    success: function (data) {
                        response($.each(data.d, function (index, value) {
                            return {
                                label: value,
                                value: index
                            }
                        }));
                    }

                });
            },
            select: function (event, ui) {
                $('#<%=txtState.ClientID%>').val(ui.value);
            },
            minLength: 2,
            delay: 500
        });

Finally, what I am actually trying to achieve is when the user selects the city in the autocomplete which is hanging from: $('#<%=txtCity.ClientID%>'), I'd like the Jquery to split the value (e.g. New Orleans, Louisiana into two (New Orleans) and (Louisiana)), I'd then like 'New Orleans' to be the value of $('#<%=txtCity.ClientID%>') and 'Louisiana' to be the value of $('#<%=txtState.ClientID%>')... Any assistance getting this crazyness to work is always appreciated :)


If I understand you clearly, you just need to return the data from the WebMethod a bit differently:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetCitiesWithState(string isoalpha2, string prefixText)
{
    var dict = AtomicCore.CityObject.GetCitiesInCountryWithStateAutocomplete(isoalpha2, prefixText);
    string[] response = dict.Select(x => String.Format("{0}, {1}", x.Key, x.Value)).ToArray();
    return response;
}

And now, at JavaScript side, you need to split the string into index and label manually (because what index is now is just a line number, I believe). Something like that (just a draft):

response($.each(data.d, function (index, value) {
    return {
        label: value.slice(value.indexOf(',')),
        value: parseInt(value.split(',')[0])
     }
 }));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜