Created
November 6, 2017 17:38
-
-
Save guibranco/e11843664b2e9163c20ad3876ba277c8 to your computer and use it in GitHub Desktop.
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
//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