我正在开发一个项目,在注册页面中我想获得一个列表 下拉列表中的所有国家/地区。任何人都可以告诉我我的意思 需要在Controller,View或其他任何需要的地方做?我有一个观点 注册页面
@model Retail_MVC.Models.registration
@{
Layout = null;
}
<div class="my_wrapper">
<div class="my_left_box">
@Html.LabelFor(model => model.username)
</div>
<div class="my_right_box">
<input type="text" id="txtusername" name="UserName"/>
</div>
</div>
-- here i want to get dropdownlist for all countries
答案 0 :(得分:3)
ASP.NET MVC框架默认有许多非常有用的帮助程序,但总是让人感到困惑的是Html.DropDownListFor()帮助程序方法。所以在这篇文章中,我会快速浏览一下我用来填充列表的步骤,以及一些启动和运行时更加时髦的功能!
在我们的示例中,表单上只会有一个下拉列表,其中显示了一个国家/地区列表,您可以从中选择并提交。
首先,我们需要构建一个视图模型,该模型将成为确定视图可以显示的数据的合约。
public class IndexViewModel
{
// Stores the selected value from the drop down box.
[Required]
public int CountryID { get; set; }
// Contains the list of countries.
public SelectList Countries { get; set; }
}
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
IndexViewModel viewModel = new IndexViewModel();
viewModel.Countries = new SelectList(GetCountries(), "ID", "Name");
return View(viewModel);
}
[HttpPost]
public ActionResult Index(IndexViewModel viewModel)
{
viewModel.Countries = new SelectList(GetCountries(), "ID", "Name");
if (!ModelState.IsValid)
{ return View(viewModel); }
//TODO: Do something with the selected country...
CMSService.UpdateCurrentLocation(viewModel.CountryID);
return View(viewModel);
}
}
@Html.DropDownListFor(x => x.CountryID, Model.Countries)
@Html.DropDownListFor(x => x.CountryID, Model.Countries, "- please select -")
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")"
type="text/css" rel="stylesheet" />
<script type="text/javascript" src="@Url.Content("></script>
<script type="text/javascript" src="@Url.Content("></script>
<link type="text/css" rel="stylesheet" href="@Url.Content("~/Content/selectmenu.css")" />
<script src="@Url.Content("~/Scripts/selectmenu.js")" type="text/javascript"></script>
<script type="text/javascript">
$('select').selectmenu();
</script>
答案 1 :(得分:3)
// Namespaces You need
using System.Globalization;
using System.Linq;
// GetCountries() method
IEnumerable<string> GetCountries()
{
return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(x => new RegionInfo(x.LCID).EnglishName)
.Distinct()
.OrderBy(x => x);
}
<强> EDITS * ////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////// *
class Country
{
public string ID { get; set; }
public string Name { get; set; }
}
IEnumerable<Country> GetCountries()
{
return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(x => new Country
{
ID = new RegionInfo(x.LCID).Name,
Name = new RegionInfo(x.LCID).EnglishName
})
.GroupBy(c => c.ID)
.Select(c => c.First())
.OrderBy(x => x.Name);
}
答案 2 :(得分:1)
尝试下面的一些事情...它会帮助你......
在HTML视图中
@{
var CountryItems = new List<SelectListItem>();
CountryItems.Add(new SelectListItem { Text = String.Empty, Value = String.Empty });
foreach (var name in ViewBag.Names)
{
CountryItems.Add(new SelectListItem { Text = name, Value = name });
}
}
@Html.DropDownList("dropDownList", CountryItems)
在控制器中:
List<string> countries =new List<string>(2);
var dtList = from ctry in objContext.Details
select ctry; // Source of Country
foreach (var dt in dtList)
{
countries.Add(dt.Name);
}
ViewBag.Names = countries ;
return View(dtList);
答案 3 :(得分:0)
在您的注册模型中,添加一个类型国家/地区的列表 - 显然这会根据您所在的国家/地区数据类型而变化
public List<Countries> Countries { get; set; }
然后在您的控制器中,根据您选择的任何数据源为您的国家/地区分配
registration.Countries = [list_of_country_objects]
最后你的看法:
@Html.DropDownList("countries", new SelectList(Model.Countries, "CountryId", "CountryName"