I'm using MVC 5.2.0 and I'm trying to use the new Html.EnumDropDownListFor
. This is how I'm setting the values:
//Model
public class MyModel {
public int SelectedEnumId { get; set; }
public TestEnum MyEnum { get; set; }
}
//Enum
public enum TestEnum : int
{
name1 = 1,
name2 = 2
}
//View
@Html.EnumDropDownListFor(model => model.MyEnum,new { @class = "form-control" })
This is working and the values are being displayed. But how do I set the selected value (SelectedEnumId)?
Normally I would use
//Not enum
@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.myvalues, "Value", "Text"))
Is there a way to do this with the new Helper in MVC 5.1-5.2? Or I have to create a Extension method for this?
@Html.EnumDropDownListFor(model => model.Foo.TestEnum);
will display the dropdown values, but nothing is ever selected. – Abrade