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

前端 未结 6 1709
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  我在风中等你
    2020-12-30 16:29

    I just stumbled on this StackO question/answer - not only is my question here an exact duplicate, but the answer given by Mike McLaughlin is the best solution I've seen for using DropdownListFor<> with Enums. Specifically, Mike points us to a solution that he found by Morgan Leroi

    Morgan's quick solution is:

    @Html.DropDownListFor(model => model.State, new SelectList(Enum.GetValues(typeof(MyNamespace.Enums.States))))
    

    But, Morgan has made an Extension that makes the implementation of the DropDownListFor<> with enums even more compact. You can download Morgan's Visual Studio Solution here.

    That said, I think we should close this question as it's an exact duplicate.

提交回复
热议问题