Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save derans/3462135 to your computer and use it in GitHub Desktop.
Save derans/3462135 to your computer and use it in GitHub Desktop.
DropDownList HtmlHelper Extension
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
var property = expression.GetProperty();
IEnumerable<SelectListItem> selectList;
var selectListProviderAttribute = property.GetAttribute<SelectListProviderAttribute>();
if (selectListProviderAttribute != null)
{
var provider = selectListProviderAttribute.Provider;
selectList = provider.Provide();
}
else
{
throw new ArgumentException(string.Format("SelectListProvider not specified for property \"{0}\"", property.Name));
}
return html.DropDownListFor(expression, selectList);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment