Skip to content

Instantly share code, notes, and snippets.

@guibranco
Created November 6, 2017 17:38
Show Gist options
  • Save guibranco/e11843664b2e9163c20ad3876ba277c8 to your computer and use it in GitHub Desktop.
Save guibranco/e11843664b2e9163c20ad3876ba277c8 to your computer and use it in GitHub Desktop.
//Enums/Test.cs
public enum Test
{
NONE = 0,
ONE = 1,
TWO = 2,
THREE = 3,
FOUR = 4,
FIVE = 5
}
//Helpers/EnumHelpers.cs
public static class EnumHelpers
{
public static IEnumerable<SelectListItem> ToSelectList()
{
return from Test e in Enum.GetValues(typeof(Test))
select new SelectListItem
{
Value =((Int32)e).ToString(),
Text = e.ToString()
};
}
}
//Controllers/ControllerController.cs
public class ControllerController : Controller
{
public ActionResult View()
{
ViewBag.Values = EnumHelpers.ToSelectList();
}
}
//Views/Controller/View.cshtml
@Html.DropDownFor(model => model.EnumProp, ViewBag.Values as IEnumerable<SelectListItem>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment