Binding a Dropdownlist on a blank form to a Model using a dictionary
In my Models folder I have a class for United States that looks like this:
public class UnitedStates
{
public static readonly IDictionary Cust_States = new Dictionary<string, object>
{{"AL", "AL"},{"AK", "AK"},{"AZ", "AZ"},{"AR", "AR"},{"CA", "CA"},{"CO", "CO"},
{"CT", "CT"},{"DE", "DE"},{"FL", "FL"},{"GA", "GA"},{"HI", "HI"},{"ID", "ID"},
{"IL", "IL"},{"IN", "IN"},{"IA", "IA"},{"KS", "KS"},{"KY", "KY"},{"LA", "LA"},
{"ME", "ME"},{"MD", "MD"},{"MA", "MA"},{"MI", "MI"},{"MN", "MN"},{"MS", "MS"},
{"MO", "MO"},{"MT", "MT"},{"NE", "NE"},{"NV", "NV"},{"NH", "NH"},{"NJ", "NJ"},
{"NM", "NM"},{"NY", "NY"},{"NC", "NC"},{"ND", "ND"},{"OH", "OH"},{"OK", "OK"},
{"OR", "OR"},{"PA", "PA"},{"RI", "RI"},{"SC", "SC"},{"SD", "SD"},{"TN", "TN"},
{"TX", "TX"},{"UT", "UT"},{"VT", "VT"},{"VA ", "VA"},{"WA", "WA"},{"WV", "WV"},
{"WI", "WI"},{"WY", "WY"}};
}
Or the longform version:
public static readonly IDictionary Cust_States = new Dictionary<string, object>
{{"AL", "ALABAMA"},{"AK", "ALASKA"},{"AZ", "ARIZONA"},{"AR", "ARKANSAS"},{"CA", "CALIFORNIA"},{"CO", "COLORADO"},
{"CT", "CONNETICUT"},{"DE", "DELAWARE"},{"FL", "FLORIDA"},{"GA", "GEORGIA"},{"HI", "HAWAII"},{"ID", "IDAHO"},
{"IL", "ILLINOIS"},{"IN", "INDIANA"},{"IA", "IOWA"},{"KS", "KANSAS"},{"KY", "KENTUCKY"},{"LA", "LOUISIANA"},
{"ME", "MAINE"},{"MD", "MARYLAND"},{"MA", "MASSACHUSETTS"},{"MI", "MICHIGAN"},{"MN", "MAINE"},{"MS", "MISSISSIPPI"},
{"MO", "MISSOURI"},{"MT", "MONTANA"},{"NE", "NEBRASKA"},{"NV", "NEVADA"},{"NH", "NEW HAMPSHIRE"},{"NJ", "NEW JERSEY"},
{"NM", "NEW MEXICO"},{"NY", "NEW YORK"},{"NC", "NORTH CAROLINA"},{"ND", "NORTH DAKOTA"},{"OH", "OHIO"},{"OK", "OKLAHOMA"},
{"OR", "OREGON"},{"PA", "PENNSYLVANIA"},{"RI", "RHODE ISLAND"},{"SC", "SOUTH CAROLINA"},{"SD", "SOUTH DAKOTA"},{"TN", "TENNESSEE"},
{"TX", "TEXAS"},{"UT", "UTAH"},{"VT", "VERMONT"},{"VA ", "VIRGINIA"},{"WA", "WASHINGTON"},{"WV", "WEST VIRGINIA"},
{"WI", "WISCONSIN"},{"WY", "WYOMING"}};
In my form for the dropdown I can bind directly to that Dictionary - where Cust_State is the value that my Model will use
@Html.DropDownList("Cust_State", new SelectList(NCC.Models.UnitedStates.Cust_States, "Key", "Value") )
public class UnitedStates
{
public static readonly IDictionary Cust_States = new Dictionary<string, object>
{{"AL", "AL"},{"AK", "AK"},{"AZ", "AZ"},{"AR", "AR"},{"CA", "CA"},{"CO", "CO"},
{"CT", "CT"},{"DE", "DE"},{"FL", "FL"},{"GA", "GA"},{"HI", "HI"},{"ID", "ID"},
{"IL", "IL"},{"IN", "IN"},{"IA", "IA"},{"KS", "KS"},{"KY", "KY"},{"LA", "LA"},
{"ME", "ME"},{"MD", "MD"},{"MA", "MA"},{"MI", "MI"},{"MN", "MN"},{"MS", "MS"},
{"MO", "MO"},{"MT", "MT"},{"NE", "NE"},{"NV", "NV"},{"NH", "NH"},{"NJ", "NJ"},
{"NM", "NM"},{"NY", "NY"},{"NC", "NC"},{"ND", "ND"},{"OH", "OH"},{"OK", "OK"},
{"OR", "OR"},{"PA", "PA"},{"RI", "RI"},{"SC", "SC"},{"SD", "SD"},{"TN", "TN"},
{"TX", "TX"},{"UT", "UT"},{"VT", "VT"},{"VA ", "VA"},{"WA", "WA"},{"WV", "WV"},
{"WI", "WI"},{"WY", "WY"}};
}
Or the longform version:
public static readonly IDictionary Cust_States = new Dictionary<string, object>
{{"AL", "ALABAMA"},{"AK", "ALASKA"},{"AZ", "ARIZONA"},{"AR", "ARKANSAS"},{"CA", "CALIFORNIA"},{"CO", "COLORADO"},
{"CT", "CONNETICUT"},{"DE", "DELAWARE"},{"FL", "FLORIDA"},{"GA", "GEORGIA"},{"HI", "HAWAII"},{"ID", "IDAHO"},
{"IL", "ILLINOIS"},{"IN", "INDIANA"},{"IA", "IOWA"},{"KS", "KANSAS"},{"KY", "KENTUCKY"},{"LA", "LOUISIANA"},
{"ME", "MAINE"},{"MD", "MARYLAND"},{"MA", "MASSACHUSETTS"},{"MI", "MICHIGAN"},{"MN", "MAINE"},{"MS", "MISSISSIPPI"},
{"MO", "MISSOURI"},{"MT", "MONTANA"},{"NE", "NEBRASKA"},{"NV", "NEVADA"},{"NH", "NEW HAMPSHIRE"},{"NJ", "NEW JERSEY"},
{"NM", "NEW MEXICO"},{"NY", "NEW YORK"},{"NC", "NORTH CAROLINA"},{"ND", "NORTH DAKOTA"},{"OH", "OHIO"},{"OK", "OKLAHOMA"},
{"OR", "OREGON"},{"PA", "PENNSYLVANIA"},{"RI", "RHODE ISLAND"},{"SC", "SOUTH CAROLINA"},{"SD", "SOUTH DAKOTA"},{"TN", "TENNESSEE"},
{"TX", "TEXAS"},{"UT", "UTAH"},{"VT", "VERMONT"},{"VA ", "VIRGINIA"},{"WA", "WASHINGTON"},{"WV", "WEST VIRGINIA"},
{"WI", "WISCONSIN"},{"WY", "WYOMING"}};
In my form for the dropdown I can bind directly to that Dictionary - where Cust_State is the value that my Model will use
@Html.DropDownList("Cust_State", new SelectList(NCC.Models.UnitedStates.Cust_States, "Key", "Value") )
Comments