Skip to content

Instantly share code, notes, and snippets.

@danielmackay
Last active December 26, 2015 12:09
Show Gist options
  • Save danielmackay/7149342 to your computer and use it in GitHub Desktop.
Save danielmackay/7149342 to your computer and use it in GitHub Desktop.
MVC select list helper for enums. #mvc
public static class SelectListExt
{
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
where TEnum : struct, IComparable, IFormattable, IConvertible
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Id = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name", enumObj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment