创建带有国家名称的下拉列表

本文关键字:下拉列表 国家 创建 | 更新日期: 2023-09-27 18:11:46

我想创建一个包含国家名称的下拉列表。

我已经试过了:

ViewBag.country =
                from p in
                    CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures)
                               .OrderBy(c => c.Name)
                select new SelectListItem
                    {
                        Text = p.EnglishName,
                        Value = p.DisplayName
                    };

但是它不是给我国家的名字,而是给我语言,然后是使用它的国家…

视图如下,简单如"hello":

Country: @Html.DropDownList("country") 

我做错了什么?谢谢!

注意我不能使用下拉列表for,因为我的视图中的国家下拉列表没有与我的模型绑定。我只是想用国家名称作为字符串填充一个下拉列表,之后我将重复使用。

创建带有国家名称的下拉列表

尝试更改

@Html.DropDownList("country") 

@Html.DropDownListFor(model => model.Country, (IEnumerable<SelectListItem>)ViewBag.country)

试试这个

DataTable Country= ViewBag.country;
var country = (from L in Country.AsEnumerable()
                       select new
                       {
                           Value = L["DisplayName"],
                           Text = L["EnglishName"]
                       });    
@Html.DropDownList("ddlCountry", new SelectList(country, "Value", "Text"), "--Select--", new { style = "width:150px" })

国家名称实际上可以从RegionInfo中检索。试试这个:

var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(c => new RegionInfo(c.LCID)).Distinct().OrderBy(c => c.EnglishName);
ViewBag.country = new SelectList(countries, "Name", "EnglishName");