List of All Countries DropDown

大憨熊 提交于 2021-02-18 10:45:34

问题


I used this code to populate my dropdownlist with countries list:

public JsonResult GetAllCountries()
{
    var objDict = new Dictionary<string, string>();
    foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
    {
        var regionInfo = new RegionInfo(cultureInfo.Name);
        if (!objDict.ContainsKey(regionInfo.EnglishName))
        {
            objDict.Add(cultureInfo.EnglishName, regionInfo.TwoLetterISORegionName.ToLower());
        }
    }
    var obj = objDict.OrderBy(p => p.Key).ToArray();

    return Json(obj.Select(t => new 
    { 
        Text = t.Key, 
        Value = t.Value 
    }), JsonRequestBehavior.AllowGet);
}

It populates This Way. And I used same code but Console, and shows differently Here. Why? And what should I do to populate the dropdownlist like the second one?


回答1:


The line

objDic.Add(cultureInfo.EnglishName, regionInfo.TwoLetterISORegionName.ToLower());

Should read

objDic.Add(regionInfo.EnglishName, regionInfo.TwoLetterISORegionName.ToLower());

This will have the website output the same as the console app



来源:https://stackoverflow.com/questions/29845769/list-of-all-countries-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!