问题
My controller action code:
ViewBag.country = from p in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures)
select new SelectListItem
{
Text = p.EnglishName,
Value = p.DisplayName
};
View code:
<dl>
<dt>
<label>
Country:
</label>
</dt>
<dd>
@Html.DropDownListFor(model => model.Country, (IEnumerable<SelectListItem>)ViewBag.country)
</dd>
<dl>
It generates a drop-down list of unsorted languages. But I need a drop-down list of sorted country list. Help Please!!!!
回答1:
Is this what you want?
ViewBag.country = from p in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures).OrderBy(c=>c.Name)
select new SelectListItem
{
Text = p.EnglishName,
Value = p.DisplayName
};
回答2:
Windows and/or .Net do not contain a list of all countries.
The list of languages/cultures is stable, countries come and go, or change name frequently.
Go to the ISO site, and you can download a list of countries from their site. You have to download this list, and update your data frequently.
Update: The list of country codes is no longer freely distributed by ISO, more information can be found on the iso.org site.
And you have to decide, if you want to include countries like Palestine on this list (Palestine is on this list, just an example of "new" countries). You would probably have to add them manually.
回答3:
you can use this list
@Html.DropDownListFor(x => x.sLA.COUNTRYCODEPHONENUMBER, new SelectList(
new[]{
new{ value="44" ,Text ="UK (+44)"},
new{ value="1",Text ="USA (+1)"},
new{ value="213",Text ="Algeria (+213)"},
new{ value="376",Text ="Andorra (+376)"},
new{ value="244",Text ="Angola (+244)"},
new{ value="1264",Text ="Anguilla (+1264)"},
new{ value="1268",Text ="Antigua & Barbuda (+1268)"},
new{ value="54",Text ="Argentina (+54)"},
new{ value="374",Text ="Armenia (+374)"},
new{ value="297",Text ="Aruba (+297)"},
new{ value="61",Text ="Australia (+61)"},
new{ value="43",Text ="Austria (+43)"},
new{ value="994",Text ="Azerbaijan (+994)"},
new{ value="1242",Text ="Bahamas (+1242)"},
new{ value="973",Text ="Bahrain (+973)"},
new{ value="880",Text ="Bangladesh (+880)"},
new{ value="1246",Text ="Barbados (+1246)"},
new{ value="375",Text ="Belarus (+375)"},
new{ value="32",Text ="Belgium (+32)"},
new{ value="501",Text ="Belize (+501)"},
new{ value="229",Text ="Benin (+229)"},
new{ value="1441",Text ="Bermuda (+1441)"},
new{ value="975",Text ="Bhutan (+975)"},
new{ value="591",Text ="Bolivia (+591)"},
new{ value="387",Text ="Bosnia Herzegovina (+387)"},
new{ value="267",Text ="Botswana (+267)"},
new{ value="55",Text ="Brazil (+55)"},
new{ value="673",Text ="Brunei (+673)"},
new{ value="359",Text ="Bulgaria (+359)"},
new{ value="226",Text ="Burkina Faso (+226)"},
new{ value="257",Text ="Burundi (+257)"},
new{ value="855",Text ="Cambodia (+855)"},
new{ value="237",Text ="Cameroon (+237)"},
new{ value="1",Text ="Canada (+1)"},
new{ value="238",Text ="Cape Verde Islands (+238)"},
new{ value="1345",Text ="Cayman Islands (+1345)"},
new{ value="236",Text ="Central African Republic (+236)"},
new{ value="56",Text ="Chile (+56)"},
new{ value="86",Text ="China (+86)"},
new{ value="57",Text ="Colombia (+57)"},
new{ value="269",Text ="Comoros (+269)"},
new{ value="242",Text ="Congo (+242)"},
new{ value="682",Text ="Cook Islands (+682)"},
new{ value="506",Text ="Costa Rica (+506)"},
new{ value="385",Text ="Croatia (+385)"},
new{ value="53",Text ="Cuba (+53)"},
new{ value="90392",Text ="Cyprus North (+90392)"},
new{ value="357",Text ="Cyprus South (+357)"},
new{ value="42",Text ="Czech Republic (+42)"},
new{ value="45",Text ="Denmark (+45)"},
new{ value="253",Text ="Djibouti (+253)"},
new{ value="1809",Text ="Dominica (+1809)"},
new{ value="1809",Text ="Dominican Republic (+1809)"},
new{ value="593",Text ="Ecuador (+593)"},
new{ value="20",Text ="Egypt (+20)"},
new{ value="503",Text ="El Salvador (+503)"},
new{ value="240",Text ="Equatorial Guinea (+240)"},
new{ value="291",Text ="Eritrea (+291)"},
new{ value="372",Text ="Estonia (+372)"},
new{ value="251",Text ="Ethiopia (+251)"},
new{ value="500",Text ="Falkland Islands (+500)"},
new{ value="298",Text ="Faroe Islands (+298)"},
new{ value="679",Text ="Fiji (+679)"},
new{ value="358",Text ="Finland (+358)"},
new{ value="33",Text ="France (+33)"},
new{ value="594",Text ="French Guiana (+594)"},
new{ value="689",Text ="French Polynesia (+689)"},
new{ value="241",Text ="Gabon (+241)"},
new{ value="220",Text ="Gambia (+220)"},
new{ value="7880",Text ="Georgia (+7880)"},
new{ value="49",Text ="Germany (+49)"},
new{ value="233",Text ="Ghana (+233)"},
new{ value="350",Text ="Gibraltar (+350)"},
new{ value="30",Text ="Greece (+30)"},
new{ value="299",Text ="Greenland (+299)"},
new{ value="1473",Text ="Grenada (+1473)"},
new{ value="590",Text ="Guadeloupe (+590)"},
new{ value="671",Text ="Guam (+671)"},
new{ value="502",Text ="Guatemala (+502)"},
new{ value="224",Text ="Guinea (+224)"},
new{ value="245",Text ="Guinea - Bissau (+245)"},
new{ value="592",Text ="Guyana (+592)"},
new{ value="509",Text ="Haiti (+509)"},
new{ value="504",Text ="Honduras (+504)"},
new{ value="852",Text ="Hong Kong (+852)"},
new{ value="36",Text ="Hungary (+36)"},
new{ value="354",Text ="Iceland (+354)"},
new{ value="91",Text ="India (+91)"},
new{ value="62",Text ="Indonesia (+62)"},
new{ value="98",Text ="Iran (+98)"},
new{ value="964",Text ="Iraq (+964)"},
new{ value="353",Text ="Ireland (+353)"},
new{ value="972",Text ="Israel (+972)"},
new{ value="39",Text ="Italy (+39)"},
new{ value="1876",Text ="Jamaica (+1876)"},
new{ value="81",Text ="Japan (+81)"},
new{ value="962",Text ="Jordan (+962)"},
new{ value="7",Text ="Kazakhstan (+7)"},
new{ value="254",Text ="Kenya (+254)"},
new{ value="686",Text ="Kiribati (+686)"},
new{ value="850",Text ="Korea North (+850)"},
new{ value="82",Text ="Korea South (+82)"},
new{ value="965",Text ="Kuwait (+965)"},
new{ value="996",Text ="Kyrgyzstan (+996)"},
new{ value="856",Text ="Laos (+856)"},
new{ value="371",Text ="Latvia (+371)"},
new{ value="961",Text ="Lebanon (+961)"},
new{ value="266",Text ="Lesotho (+266)"},
new{ value="231",Text ="Liberia (+231)"},
new{ value="218",Text ="Libya (+218)"},
new{ value="417",Text ="Liechtenstein (+417)"},
new{ value="370",Text ="Lithuania (+370)"},
new{ value="352",Text ="Luxembourg (+352)"},
new{ value="853",Text ="Macao (+853)"},
new{ value="389",Text ="Macedonia (+389)"},
new{ value="261",Text ="Madagascar (+261)"},
new{ value="265",Text ="Malawi (+265)"},
new{ value="60",Text ="Malaysia (+60)"},
new{ value="960",Text ="Maldives (+960)"},
new{ value="223",Text ="Mali (+223)"},
new{ value="356",Text ="Malta (+356)"},
new{ value="692",Text ="Marshall Islands (+692)"},
new{ value="596",Text ="Martinique (+596)"},
new{ value="222",Text ="Mauritania (+222)"},
new{ value="269",Text ="Mayotte (+269)"},
new{ value="52",Text ="Mexico (+52)"},
new{ value="691",Text ="Micronesia (+691)"},
new{ value="373",Text ="Moldova (+373)"},
new{ value="377",Text ="Monaco (+377)"},
new{ value="976",Text ="Mongolia (+976)"},
new{ value="1664",Text ="Montserrat (+1664)"},
new{ value="212",Text ="Morocco (+212)"},
new{ value="258",Text ="Mozambique (+258)"},
new{ value="95",Text ="Myanmar (+95)"},
new{ value="264",Text ="Namibia (+264)"},
new{ value="674",Text ="Nauru (+674)"},
new{ value="977",Text ="Nepal (+977)"},
new{ value="31",Text ="Netherlands (+31)"},
new{ value="687",Text ="New Caledonia (+687)"},
new{ value="64",Text ="New Zealand (+64)"},
new{ value="505",Text ="Nicaragua (+505)"},
new{ value="227",Text ="Niger (+227)"},
new{ value="234",Text ="Nigeria (+234)"},
new{ value="683",Text ="Niue (+683)"},
new{ value="672",Text ="Norfolk Islands (+672)"},
new{ value="670",Text ="Northern Marianas (+670)"},
new{ value="47",Text ="Norway (+47)"},
new{ value="968",Text ="Oman (+968)"},
new{ value="680",Text ="Palau (+680)"},
new{ value="507",Text ="Panama (+507)"},
new{ value="675",Text ="Papua New Guinea (+675)"},
new{ value="595",Text ="Paraguay (+595)"},
new{ value="51",Text ="Peru (+51)"},
new{ value="63",Text ="Philippines (+63)"},
new{ value="48",Text ="Poland (+48)"},
new{ value="351",Text ="Portugal (+351)"},
new{ value="1787",Text ="Puerto Rico (+1787)"},
new{ value="974",Text ="Qatar (+974)"},
new{ value="262",Text ="Reunion (+262)"},
new{ value="40",Text ="Romania (+40)"},
new{ value="7",Text ="Russia (+7)"},
new{ value="250",Text ="Rwanda (+250)"},
new{ value="378",Text ="San Marino (+378)"},
new{ value="239",Text ="Sao Tome & Principe (+239)"},
new{ value="966",Text ="Saudi Arabia (+966)"},
new{ value="221",Text ="Senegal (+221)"},
new{ value="381",Text ="Serbia (+381)"},
new{ value="248",Text ="Seychelles (+248)"},
new{ value="232",Text ="Sierra Leone (+232)"},
new{ value="65",Text ="Singapore (+65)"},
new{ value="421",Text ="Slovak Republic (+421)"},
new{ value="386",Text ="Slovenia (+386)"},
new{ value="677",Text ="Solomon Islands (+677)"},
new{ value="252",Text ="Somalia (+252)"},
new{ value="27",Text ="South Africa (+27)"},
new{ value="34",Text ="Spain (+34)"},
new{ value="94",Text ="Sri Lanka (+94)"},
new{ value="290",Text ="St. Helena (+290)"},
new{ value="1869",Text ="St. Kitts (+1869)"},
new{ value="1758",Text ="St. Lucia (+1758)"},
new{ value="249",Text ="Sudan (+249)"},
new{ value="597",Text ="Suriname (+597)"},
new{ value="268",Text ="Swaziland (+268)"},
new{ value="46",Text ="Sweden (+46)"},
new{ value="41",Text ="Switzerland (+41)"},
new{ value="963",Text ="Syria (+963)"},
new{ value="886",Text ="Taiwan (+886)"},
new{ value="7",Text ="Tajikstan (+7)"},
new{ value="66",Text ="Thailand (+66)"},
new{ value="228",Text ="Togo (+228)"},
new{ value="676",Text ="Tonga (+676)"},
new{ value="1868",Text ="Trinidad & Tobago (+1868)"},
new{ value="216",Text ="Tunisia (+216)"},
new{ value="90",Text ="Turkey (+90)"},
new{ value="7",Text ="Turkmenistan (+7)"},
new{ value="993",Text ="Turkmenistan (+993)"},
new{ value="1649",Text ="Turks & Caicos Islands (+1649)"},
new{ value="688",Text ="Tuvalu (+688)"},
new{ value="256",Text ="Uganda (+256)"},
new{ value="44",Text ="UK (+44)"},
new{ value="380",Text ="Ukraine (+380)"},
new{ value="971",Text ="United Arab Emirates (+971)"},
new{ value="598",Text ="Uruguay (+598)"},
new{ value="1",Text ="USA (+1)"},
new{ value="7",Text ="Uzbekistan (+7)"},
new{ value="678",Text ="Vanuatu (+678)"},
new{ value="379",Text ="Vatican City (+379)"},
new{ value="58",Text ="Venezuela (+58)"},
new{ value="84",Text ="Vietnam (+84)"},
new{ value="84",Text ="Virgin Islands - British (+1284)"},
new{ value="84",Text ="Virgin Islands - US (+1340)"},
new{ value="681",Text ="Wallis & Futuna (+681)"},
new{ value="969",Text ="Yemen (North)(+969)"},
new{ value="967",Text ="Yemen (South)(+967)"},
new{ value="260",Text ="Zambia (+260)"},
new{ value="263",Text ="Zimbabwe (+263)"},
},
"Value",
"Text",
Model), new { @class = "form-control" })
来源:https://stackoverflow.com/questions/7763240/asp-net-mvc-3-mvc-3-razor-country-selection-drop-down-list