views:

4310

answers:

3

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

A: 

You can build this with an extension method pretty easily.

Craig
Care to show us how?
UpTheCreek
A: 

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.

Kyle West
+24  A: 

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