加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 如何在mvc3的dropdownlist中获取国家/地区列表

发布时间:2020-12-16 06:32:03 所属栏目:asp.Net 来源:网络整理
导读:我正在开发一个项目,在注册页面中我想获得一个列表 ?下拉列表中的所有国家/地区.任何人都可以告诉我我的意思 ?需要在Controller,View或其他任何需要的地方做?我有一个观点 注册页面 @model Retail_MVC.Models.registration@{ Layout = null;}div class="my_
我正在开发一个项目,在注册页面中我想获得一个列表
?下拉列表中的所有国家/地区.任何人都可以告诉我我的意思
?需要在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

解决方法

使用 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);
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读