Skip to content

Instantly share code, notes, and snippets.

@andrewbranch
Last active November 4, 2023 11:01
Show Gist options
  • Save andrewbranch/6234651 to your computer and use it in GitHub Desktop.
Save andrewbranch/6234651 to your computer and use it in GitHub Desktop.
Easy state and country dropdowns for MVC .NET, current as of 8/2013
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<% string defaultCountry = "United States";
if (ViewData.ContainsKey("default")) {
defaultCountry = (string)ViewData["default"];
}
%>
<select id="<%= Html.IdFor(model => model) %>" name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>"<% if (ViewData.ContainsKey("html")) { foreach (var a in HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["html"])) { %> <%= a.Key %>="<%= a.Value %>"<% } } %>>
<% if (String.IsNullOrWhiteSpace(Model) && !Inspect.Models.Location.Countries.Keys.Contains(defaultCountry)) { %><option value="<%: defaultCountry %>"><%: defaultCountry %></option><% } %>
<% foreach (var country in Inspect.Models.Location.Countries.Keys) { %>
<option value="<%: country %>"<% if (country == Model || String.IsNullOrWhiteSpace(Model) && country == defaultCountry) { %> selected="selected"<% } %>><%: Inspect.Models.Location.Countries[country] %></option>
<% } %>
</select>
<%= Html.EditorFor(model => model.Country, "Country", new { default = "France", html = new { @class = "form-control", data_bind="value: whatever } } %>
<%= Html.EditorFor(model => model.State, "State", new { html = new { @class = "form-control", data_bind="value: whatever } } %>
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
namespace Inspect.Models {
[ComplexType]
public class Location {
public string Room { get; set; }
public string Building { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
[MaxLength(10, ErrorMessage = "Zip code must be 5 or 9 digits")]
public string Zip { get; set; }
public string Country { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public static Dictionary<string, string> Countries = new Dictionary<string,string>() {
{ "Afghanistan", "Afghanistan" },
{ "Albania", "Albania" },
{ "Algeria", "Algeria" },
{ "American Samoa", "American Samoa" },
{ "Andorra", "Andorra" },
{ "Angola", "Angola" },
{ "Anguilla", "Anguilla" },
{ "Antarctica", "Antarctica" },
{ "Antigua and Barbuda", "Antigua and Barbuda" },
{ "Argentina", "Argentina" },
{ "Armenia", "Armenia" },
{ "Aruba", "Aruba" },
{ "Australia", "Australia" },
{ "Austria", "Austria" },
{ "Azerbaijan", "Azerbaijan" },
{ "Bahamas", "Bahamas" },
{ "Bahrain", "Bahrain" },
{ "Bangladesh", "Bangladesh" },
{ "Barbados", "Barbados" },
{ "Belarus", "Belarus" },
{ "Belgium", "Belgium" },
{ "Belize", "Belize" },
{ "Benin", "Benin" },
{ "Bermuda", "Bermuda" },
{ "Bhutan", "Bhutan" },
{ "Bolivia", "Bolivia" },
{ "Bosnia and Herzegovina", "Bosnia and Herzegovina" },
{ "Botswana", "Botswana" },
{ "Bouvet Island", "Bouvet Island" },
{ "Brazil", "Brazil" },
{ "British Indian Ocean Territory", "British Indian Ocean Territory" },
{ "Brunei Darussalam", "Brunei Darussalam" },
{ "Bulgaria", "Bulgaria" },
{ "Burkina Faso", "Burkina Faso" },
{ "Burundi", "Burundi" },
{ "Cambodia", "Cambodia" },
{ "Cameroon", "Cameroon" },
{ "Canada", "Canada" },
{ "Cape Verde", "Cape Verde" },
{ "Cayman Islands", "Cayman Islands" },
{ "Central African Republic", "Central African Republic" },
{ "Chad", "Chad" },
{ "Chile", "Chile" },
{ "China", "China" },
{ "Christmas Island", "Christmas Island" },
{ "Cocos (Keeling) Islands", "Cocos (Keeling) Islands" },
{ "Colombia", "Colombia" },
{ "Comoros", "Comoros" },
{ "Congo", "Congo" },
{ "Congo, The Democratic Republic of The", "Congo, The Democratic Republic of The" },
{ "Cook Islands", "Cook Islands" },
{ "Costa Rica", "Costa Rica" },
{ "Cote D'ivoire", "Cote D'ivoire" },
{ "Croatia", "Croatia" },
{ "Cuba", "Cuba" },
{ "Cyprus", "Cyprus" },
{ "Czech Republic", "Czech Republic" },
{ "Denmark", "Denmark" },
{ "Djibouti", "Djibouti" },
{ "Dominica", "Dominica" },
{ "Dominican Republic", "Dominican Republic" },
{ "Ecuador", "Ecuador" },
{ "Egypt", "Egypt" },
{ "El Salvador", "El Salvador" },
{ "Equatorial Guinea", "Equatorial Guinea" },
{ "Eritrea", "Eritrea" },
{ "Estonia", "Estonia" },
{ "Ethiopia", "Ethiopia" },
{ "Falkland Islands (Malvinas)", "Falkland Islands (Malvinas)" },
{ "Faroe Islands", "Faroe Islands" },
{ "Fiji", "Fiji" },
{ "Finland", "Finland" },
{ "France", "France" },
{ "French Guiana", "French Guiana" },
{ "French Polynesia", "French Polynesia" },
{ "French Southern Territories", "French Southern Territories" },
{ "Gabon", "Gabon" },
{ "Gambia", "Gambia" },
{ "Georgia", "Georgia" },
{ "Germany", "Germany" },
{ "Ghana", "Ghana" },
{ "Gibraltar", "Gibraltar" },
{ "Greece", "Greece" },
{ "Greenland", "Greenland" },
{ "Grenada", "Grenada" },
{ "Guadeloupe", "Guadeloupe" },
{ "Guam", "Guam" },
{ "Guatemala", "Guatemala" },
{ "Guinea", "Guinea" },
{ "Guinea-bissau", "Guinea-bissau" },
{ "Guyana", "Guyana" },
{ "Haiti", "Haiti" },
{ "Heard Island and Mcdonald Islands", "Heard Island and Mcdonald Islands" },
{ "Holy See (Vatican City State)", "Holy See (Vatican City State)" },
{ "Honduras", "Honduras" },
{ "Hong Kong", "Hong Kong" },
{ "Hungary", "Hungary" },
{ "Iceland", "Iceland" },
{ "India", "India" },
{ "Indonesia", "Indonesia" },
{ "Iran, Islamic Republic of", "Iran, Islamic Republic of" },
{ "Iraq", "Iraq" },
{ "Ireland", "Ireland" },
{ "Israel", "Israel" },
{ "Italy", "Italy" },
{ "Jamaica", "Jamaica" },
{ "Japan", "Japan" },
{ "Jordan", "Jordan" },
{ "Kazakhstan", "Kazakhstan" },
{ "Kenya", "Kenya" },
{ "Kiribati", "Kiribati" },
{ "Korea, Democratic People's Republic of", "Korea, Democratic People's Republic of" },
{ "Korea, Republic of", "Korea, Republic of" },
{ "Kuwait", "Kuwait" },
{ "Kyrgyzstan", "Kyrgyzstan" },
{ "Lao People's Democratic Republic", "Lao People's Democratic Republic" },
{ "Latvia", "Latvia" },
{ "Lebanon", "Lebanon" },
{ "Lesotho", "Lesotho" },
{ "Liberia", "Liberia" },
{ "Libyan Arab Jamahiriya", "Libyan Arab Jamahiriya" },
{ "Liechtenstein", "Liechtenstein" },
{ "Lithuania", "Lithuania" },
{ "Luxembourg", "Luxembourg" },
{ "Macao", "Macao" },
{ "Macedonia, The Former Yugoslav Republic of", "Macedonia, The Former Yugoslav Republic of" },
{ "Madagascar", "Madagascar" },
{ "Malawi", "Malawi" },
{ "Malaysia", "Malaysia" },
{ "Maldives", "Maldives" },
{ "Mali", "Mali" },
{ "Malta", "Malta" },
{ "Marshall Islands", "Marshall Islands" },
{ "Martinique", "Martinique" },
{ "Mauritania", "Mauritania" },
{ "Mauritius", "Mauritius" },
{ "Mayotte", "Mayotte" },
{ "Mexico", "Mexico" },
{ "Micronesia, Federated States of", "Micronesia, Federated States of" },
{ "Moldova, Republic of", "Moldova, Republic of" },
{ "Monaco", "Monaco" },
{ "Mongolia", "Mongolia" },
{ "Montserrat", "Montserrat" },
{ "Morocco", "Morocco" },
{ "Mozambique", "Mozambique" },
{ "Myanmar", "Myanmar" },
{ "Namibia", "Namibia" },
{ "Nauru", "Nauru" },
{ "Nepal", "Nepal" },
{ "Netherlands", "Netherlands" },
{ "Netherlands Antilles", "Netherlands Antilles" },
{ "New Caledonia", "New Caledonia" },
{ "New Zealand", "New Zealand" },
{ "Nicaragua", "Nicaragua" },
{ "Niger", "Niger" },
{ "Nigeria", "Nigeria" },
{ "Niue", "Niue" },
{ "Norfolk Island", "Norfolk Island" },
{ "Northern Mariana Islands", "Northern Mariana Islands" },
{ "Norway", "Norway" },
{ "Oman", "Oman" },
{ "Pakistan", "Pakistan" },
{ "Palau", "Palau" },
{ "Palestinian Territory, Occupied", "Palestinian Territory, Occupied" },
{ "Panama", "Panama" },
{ "Papua New Guinea", "Papua New Guinea" },
{ "Paraguay", "Paraguay" },
{ "Peru", "Peru" },
{ "Philippines", "Philippines" },
{ "Pitcairn", "Pitcairn" },
{ "Poland", "Poland" },
{ "Portugal", "Portugal" },
{ "Puerto Rico", "Puerto Rico" },
{ "Qatar", "Qatar" },
{ "Reunion", "Reunion" },
{ "Romania", "Romania" },
{ "Russian Federation", "Russian Federation" },
{ "Rwanda", "Rwanda" },
{ "Saint Helena", "Saint Helena" },
{ "Saint Kitts and Nevis", "Saint Kitts and Nevis" },
{ "Saint Lucia", "Saint Lucia" },
{ "Saint Pierre and Miquelon", "Saint Pierre and Miquelon" },
{ "Saint Vincent and The Grenadines", "Saint Vincent and The Grenadines" },
{ "Samoa", "Samoa" },
{ "San Marino", "San Marino" },
{ "Sao Tome and Principe", "Sao Tome and Principe" },
{ "Saudi Arabia", "Saudi Arabia" },
{ "Senegal", "Senegal" },
{ "Serbia and Montenegro", "Serbia and Montenegro" },
{ "Seychelles", "Seychelles" },
{ "Sierra Leone", "Sierra Leone" },
{ "Singapore", "Singapore" },
{ "Slovakia", "Slovakia" },
{ "Slovenia", "Slovenia" },
{ "Solomon Islands", "Solomon Islands" },
{ "Somalia", "Somalia" },
{ "South Africa", "South Africa" },
{ "South Georgia and The South Sandwich Islands", "South Georgia and The South Sandwich Islands" },
{ "Spain", "Spain" },
{ "Sri Lanka", "Sri Lanka" },
{ "Sudan", "Sudan" },
{ "Suriname", "Suriname" },
{ "Svalbard and Jan Mayen", "Svalbard and Jan Mayen" },
{ "Swaziland", "Swaziland" },
{ "Sweden", "Sweden" },
{ "Switzerland", "Switzerland" },
{ "Syrian Arab Republic", "Syrian Arab Republic" },
{ "Taiwan, Province of China", "Taiwan, Province of China" },
{ "Tajikistan", "Tajikistan" },
{ "Tanzania, United Republic of", "Tanzania, United Republic of" },
{ "Thailand", "Thailand" },
{ "Timor-leste", "Timor-leste" },
{ "Togo", "Togo" },
{ "Tokelau", "Tokelau" },
{ "Tonga", "Tonga" },
{ "Trinidad and Tobago", "Trinidad and Tobago" },
{ "Tunisia", "Tunisia" },
{ "Turkey", "Turkey" },
{ "Turkmenistan", "Turkmenistan" },
{ "Turks and Caicos Islands", "Turks and Caicos Islands" },
{ "Tuvalu", "Tuvalu" },
{ "Uganda", "Uganda" },
{ "Ukraine", "Ukraine" },
{ "United Arab Emirates", "United Arab Emirates" },
{ "United Kingdom", "United Kingdom" },
{ "United States", "United States" },
{ "United States Minor Outlying Islands", "United States Minor Outlying Islands" },
{ "Uruguay", "Uruguay" },
{ "Uzbekistan", "Uzbekistan" },
{ "Vanuatu", "Vanuatu" },
{ "Venezuela", "Venezuela" },
{ "Viet Nam", "Viet Nam" },
{ "Virgin Islands, British", "Virgin Islands, British" },
{ "Virgin Islands, U.S.", "Virgin Islands, U.S." },
{ "Wallis and Futuna", "Wallis and Futuna" },
{ "Western Sahara", "Western Sahara" },
{ "Yemen", "Yemen" },
{ "Zambia", "Zambia" },
{ "Zimbabwe", "Zimbabwe" }
};
public static Dictionary<string, Dictionary<string, string>> States = new Dictionary<string, Dictionary<string, string>>() {
{ "Outlying Territories", new Dictionary<string, string>() {
{ "AS", "American Samoa" },
{ "GU", "Guam" },
{ "MP", "Northern Mariana Islands" },
{ "PR", "Puerto Rico" },
{ "UM", "United States Minor Outlying Islands" },
{ "VI", "Virgin Islands" }
} },
{ "Armed Forces", new Dictionary<string, string>() {
{ "AA", "Armed Forces Americas" },
{ "AP", "Armed Forces Pacific" },
{ "AE", "Armed Forces Others" }
} },
{ "States and D.C.", new Dictionary<string, string>() {
{ "AL", "Alabama" },
{ "AK", "Alaska" },
{ "AZ", "Arizona" },
{ "AR", "Arkansas" },
{ "CA", "California" },
{ "CO", "Colorado" },
{ "CT", "Connecticut" },
{ "DE", "Delaware" },
{ "DC", "District of Columbia" },
{ "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", "Minnesota" },
{ "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" }
} }
};
}
}
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<select id="<%= Html.IdFor(model => model) %>" name="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>"<% if (ViewData.ContainsKey("html")) { foreach (var a in HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["html"])) { %> <%= a.Key %>="<%= a.Value %>"<% } } %>>
<option value=""></option>
<% foreach (var group in Inspect.Models.Location.States.Keys) { %>
<optgroup label="<%: group %>">
<% foreach (var state in Inspect.Models.Location.States[group].Keys) { %>
<option value="<%: state %>"<% if (state == Model) { %> selected="selected"<% } %>><%: Inspect.Models.Location.States[group][state] %></option>
<% } %>
</optgroup>
<% } %>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment