Search code examples
asp.net-mvc-3razordrop-down-menucountry

asp.net mvc 3 - MVC 3 Razor: Country selection drop down list


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!!!!


Solution

  • 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
                              };