4

我正在做一个项目,在注册页面中,我想在下拉列表中获取所有国家/地区的列表。谁能告诉我我需要在控制器、视图或任何需要的东西中做什么?我有一个注册页面的视图

@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
4

4 回答 4

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>
于 2013-01-29T12:25:29.310 回答
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);
}

编辑 * //////////////////////////////////////// ///////////////////////////////////////// ///////////////////////////////////////// ////////////// *

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);
}
于 2013-01-29T13:13:24.283 回答
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);
于 2013-01-29T12:23:30.030 回答
0

在您的注册模型中,添加类型国家/地区列表 - 显然这将根据您的国家/地区数据类型而改变

public List<Countries> Countries { get; set; }

然后在您的控制器中,从您选择的任何数据源将您的国家/地区分配给它

registration.Countries = [list_of_country_objects]

最后你的观点:

@Html.DropDownList("countries", new SelectList(Model.Countries, "CountryId", "CountryName"
于 2013-01-29T12:23:01.370 回答