// Build your normal dictionary as container
Dictionary<string, string> countryNames = new Dictionary<string, string>();
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo ri = new RegionInfo(ci.Name);
// Check if the ISO language code is already in your collection
// Because you don't want double entries in a country box because we had to use the culture info
if (!countryNames.ContainsKey(ri.TwoLetterISORegionName))
{
countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName);
}
}
// Code for dictionary to dropdownlist transform can be written with your personal preference for symantics
SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value");
Hoặc sẵn sàng để sử dụng mà không cần bình luận:
Dictionary<string, string> countryNames = new Dictionary<string, string>();
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo ri = new RegionInfo(ci.Name);
if (!countryNames.ContainsKey(ri.TwoLetterISORegionName)) countryNames.Add(ri.TwoLetterISORegionName.ToUpper(), ri.EnglishName);
}
SelectList countryDropdown = new SelectList(countryNames.OrderBy(o => o.Value), "Key", "Value");
Tên hiển thị cung cấp các tên như "tiếng Đức", "Tiếng Catalan", "Phần Lan" vv Đây không phải là tên quốc gia chính xác. Nếu không, chúng tôi có thể sử dụng DisplayName hoặc EnglishName. –
Trong hầu hết các trường hợp DisplayName bao gồm tên quốc gia/khu vực được bao quanh bởi dấu ngoặc đơn, đó là phần cuối cùng mà chúng tôi đang nhận được với cụm từ thông dụng. Một chút hack, nhưng nó hoạt động :-) –