How to use MVC Html Helper .DropDownListFor<> with an Enum

前端 未结 6 1710
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 16:01

In my MVC 3 Razor app, I have a Model with an enum..

Model Example:

public class EmployeeModel
{
 public enum Title
 {
  Accountant = 111,
  Sales =          


        
6条回答
  •  猫巷女王i
    2020-12-30 16:52

    Here is the solution

    public enum Months
    {
    January=1,
    February=2,
    March=3,
    April =4,
    May=5,
    June=6
    }
    
    public ActionResult Index()
    {
    
    ViewBag.Months = (from Months m in Enum.GetValues(typeof(Months))
                    select new SelectListItem { Text = m.ToString(), Value = Convert.ToUInt16(m).ToString() });
    
    return View();  
    }
    

    Add ViewBag name in DropDownList :

    <%=Html.DropDownList("Months") %>
    

提交回复
热议问题