You create a list from your Enum, as this stream offers or this and create a new SelectList with it and save it in the ViewBag, and then in your view make DropDownList or DropDownList for the helper and use the same name for the ViewBag variable as for the select element.
//GET: ~/ValueController/ValueAction public ActionResult ValueAction() { Array values = Enum.GetValues(typeof(ValueEnum)); List<ListItem> items = new List<ListItem>(values.Length); foreach(var i in values) { items.Add(new ListItem { Text = Enum.GetName(typeof(ValueEnum), i), Value = ((int)i).ToString() }); } ViewBag.valueEnum = new SelectList(items); }
View:
@Html.DropDownList("valueEnum", null, htmlAttributes: new { @class = "form-control" })
Then MVC will automatically assign the contents of the ViewBag to the select element.
Then, in your Post action, you set its parameters to get a prime integer.
//POST: ~/ValueController/ValueAction [HttpPost] public ActionResult ValueAction(int valueEnum) { //less code return View(); }
source share