How to get a list of countries in a drop down list in mvc3?

I am working on a project, and on the registration page I want to get a list of all countries in the drop-down list. Can someone please show me what I need to do in the controller, in the view, or anywhere else? I mean the registration page

@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
+5
source share
4 answers

There are a number of very useful helpers that are used by default in the ASP.NET MVC framework, but one that is always confusing is the Html.DropDownListFor () helper method. So, in this post, I quickly move on to the steps that I use to populate the list, as well as to some more functional functions after it starts!

, , .

, , , .

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>
+3

CultureInfo

// 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);
}
+3

- ... ...

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);
+1

Add a list of countries of type in your registration model - obviously, this will change depending on what type of data of your country looks like

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

Then in your controller, assign your countries to it from any data source that you have selected

registration.Countries = [list_of_country_objects]

And finally, your opinion:

@Html.DropDownList("countries", new SelectList(Model.Countries, "CountryId", "CountryName"
0
source

All Articles