Skip to content

Instantly share code, notes, and snippets.

@keroger2k
Created September 15, 2012 02:24
Show Gist options
  • Select an option

  • Save keroger2k/3726104 to your computer and use it in GitHub Desktop.

Select an option

Save keroger2k/3726104 to your computer and use it in GitHub Desktop.
EnumToSelectList
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { Value = e, Text = e.ToString() };
return new SelectList(values, "Value", "Text", enumObj);
}
public enum QueryTypes
{
Web,
Images,
Documents
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Select = typeof(QueryTypes).ToSelectList();
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment