Tuesday, March 1, 2011

ASP.NET MVC US State Drop Down List

anyone have have a dropdownlist helper method with a list of US states? thanks.

From stackoverflow
  • You can build this with an extension method pretty easily.

    UpTheCreek : Care to show us how?
  • I'm sure it's a pretty common request. Would be nice to have it somewhere and indexed so we can all use it. If there's nothing by tomorrow I'll write it and post it up.

  • As promised ... toss this in your "Global" namespace (or wherever else you want).

    public class UnitedStatesStates
    {
        public static readonly IDictionary<string, string> StateDictionary = new Dictionary<string, string> {
            {"ALABAMA", "AL"},
            {"ALASKA", "AK"},
            {"AMERICAN SAMOA", "AS"},
            {"ARIZONA ", "AZ"},
            {"ARKANSAS", "AR"},
            {"CALIFORNIA ", "CA"},
            {"COLORADO ", "CO"},
            {"CONNECTICUT", "CT"},
            {"DELAWARE", "DE"},
            {"DISTRICT OF COLUMBIA", "DC"},
            {"FEDERATED STATES OF MICRONESIA", "FM"},
            {"FLORIDA", "FL"},
            {"GEORGIA", "GA"},
            {"GUAM ", "GU"},
            {"HAWAII", "HI"},
            {"IDAHO", "ID"},
            {"ILLINOIS", "IL"},
            {"INDIANA", "IN"},
            {"IOWA", "IA"},
            {"KANSAS", "KS"},
            {"KENTUCKY", "KY"},
            {"LOUISIANA", "LA"},
            {"MAINE", "ME"},
            {"MARSHALL ISLANDS", "MH"},
            {"MARYLAND", "MD"},
            {"MASSACHUSETTS", "MA"},
            {"MICHIGAN", "MI"},
            {"MINNESOTA", "MN"},
            {"MISSISSIPPI", "MS"},
            {"MISSOURI", "MO"},
            {"MONTANA", "MT"},
            {"NEBRASKA", "NE"},
            {"NEVADA", "NV"},
            {"NEW HAMPSHIRE", "NH"},
            {"NEW JERSEY", "NJ"},
            {"NEW MEXICO", "NM"},
            {"NEW YORK", "NY"},
            {"NORTH CAROLINA", "NC"},
            {"NORTH DAKOTA", "ND"},
            {"NORTHERN MARIANA ISLANDS", "MP"},
            {"OHIO", "OH"},
            {"OKLAHOMA", "OK"},
            {"OREGON", "OR"},
            {"PALAU", "PW"},
            {"PENNSYLVANIA", "PA"},
            {"PUERTO RICO", "PR"},
            {"RHODE ISLAND", "RI"},
            {"SOUTH CAROLINA", "SC"},
            {"SOUTH DAKOTA", "SD"},
            {"TENNESSEE", "TN"},
            {"TEXAS", "TX"},
            {"UTAH", "UT"},
            {"VERMONT", "VT"},
            {"VIRGIN ISLANDS", "VI"},
            {"VIRGINIA", "VA"},
            {"WASHINGTON", "WA"},
            {"WEST VIRGINIA", "WV"},
            {"WISCONSIN", "WI"},
            {"WYOMING", "WY"}
        };
    
        public static SelectList StateSelectList
        {
            get { return new SelectList(StateDictionary, "Value", "Key"); }
        }
    }
    

    and use it like this:

    <%= Html.DropDownList("state", UnitedStatesStates.StateSelectList)%>
    
    Rake36 : Sweet. Thanks Kyle. Saved me some work. I love this site.
    robnardo : This helped me understand how to set name/value (or key/value) pairs for the View's select tag. thanks!
    UpTheCreek : What exactly do you mean by 'Global' namespace? Any what are best practices here regarding MVC and accessing from views?
    Kyle West : I just have a "MyApp.Global" namespace that I put stuff like this in. You can put it where ever you like.
    Josh Stodola : Why does it SHOUT the state names?

0 comments:

Post a Comment