anyone have have a dropdownlist helper method with a list of US states? thanks.
Care to show us how?
UpTheCreek
2009-10-07 08:23:11
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
2008-11-14 02:32:09
+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
2008-11-14 22:04:57
This helped me understand how to set name/value (or key/value) pairs for the View's select tag. thanks!
robnardo
2009-08-18 15:24:41
What exactly do you mean by 'Global' namespace? Any what are best practices here regarding MVC and accessing from views?
UpTheCreek
2009-10-07 09:04:54
I just have a "MyApp.Global" namespace that I put stuff like this in. You can put it where ever you like.
Kyle West
2009-10-09 21:43:53