Created
August 25, 2012 07:34
-
-
Save derans/3462135 to your computer and use it in GitHub Desktop.
DropDownList HtmlHelper Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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