Skip to content

Instantly share code, notes, and snippets.

View andrewbranch's full-sized avatar

Andrew Branch andrewbranch

View GitHub Profile
@andrewbranch
andrewbranch / Country.ascx
Last active November 4, 2023 11:01
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) { %>
@andrewbranch
andrewbranch / ErrorsController.cs
Last active December 18, 2015 02:18
Files (or snippets of files) to set up a highly flexible custom errors system that works for jQuery AJAX and synchronous calls alike for an ASP.NET MVC project.
using System.Net;
using System.Web.Mvc;
namespace ProjectName.Controllers
{
public class ErrorController : Controller
{
public ViewResult NotFound() {
Response.StatusCode = (int)HttpStatusCode.NotFound;