ASP.NET MVC加拿大省下拉列表

时间:2009-09-03 21:03:27

标签: asp.net-mvc drop-down-menu

任何人都有一个带有加拿大省份列表的下拉列表帮助方法吗?

基于此question我希望找到一个加拿大省的等价物。希望这可以节省一些额外的打字,或者导致更好的答案。

2 个答案:

答案 0 :(得分:7)

public class CanadianProvinces
{
    public static readonly IDictionary<string, string> ProvinceDictionary = new Dictionary<string, string>
                                                                             {
                                                                                 {"ALBERTA", "AB"},
                                                                                 {"BRITISH COLUMBIA", "BC"},
                                                                                 {"MANITOBA", "MB"},
                                                                                 {"NEW BRUNSWICK", "NB"},
                                                                                 {"NEWFOUNDLAND AND LABRADOR", "NL"},
                                                                                 {"NORTHWEST TERRITORIES", "NT"},
                                                                                 {"NOVA SCOTIA", "NS"},
                                                                                 {"NUNAVUT", "NU"},
                                                                                 {"ONTARIO", "ON"},
                                                                                 {"PRINCE EDWARD ISLAND", "PE"},
                                                                                 {"QUEBEC", "QC"},
                                                                                 {"SASKATCHEWAN", "SK"},
                                                                                 {"YUKON", "YT"},
                                                                             };

    public static SelectList ProvinceSelectList
    {
        get { return new SelectList(ProvinceDictionary, "Value", "Key"); }
    }

}

然后按以下方式使用:

<%= Html.DropDownList("province", CanadianProvinces.ProvinceSelectList)%>

答案 1 :(得分:4)

混合大小写以防有人需要它(编辑是否有拼写错误):

public class CanadianProvinces
{
    public static readonly IDictionary<string, string> ProvinceDictionary =
        new Dictionary<string, string>
            {
                {"Alberta", "AB"},
                {"British Columbia", "BC"},
                {"Manitoba", "MB"},
                {"New Brunswick", "NB"},
                {"Newfoundland and Labrador", "NL"},
                {"Northwest Territories", "NT"},
                {"Nova Scotia", "NS"},
                {"Nunavut", "NU"},
                {"Ontario", "ON"},
                {"Prince Edward Island", "PE"},
                {"Quebec", "QC"},
                {"Saskatchewan", "SK"},
                {"Yukon", "YT"}
            };

    public static SelectList ProvinceSelectList
    {
        get { return new SelectList(ProvinceDictionary, "Value", "Key"); }
    }
}