c# - ASP MVC 5 @Html.EnumDropDownListFor Defaults to first Item -


i have following enum:

public enum employmenttype {   fulltime,   parttime,   contract }  public class myviewmodel {    public string searchterm { get; set; }    public employmenttype employmenttype { get; set; } }   public actionresult index(string searchterm, string employmenttype) {      // other stuff       var viewmodel = new myviewmodel {  searchterm = search };       return view(viewmodel);       }  @html.enumdropdownlistfor(m => m.employmenttype, "", new { @class = "form-control" }) 

when load page dropdown default first item fulltime instead of empty option. not setting default value in controller why default first item , how can default empty option value instead?

the solution make model enum property nullable type:

public class myviewmodel {    public string searchterm { get; set; }    public employmenttype? employmenttype { get; set; } } 

this way property allows null entries , default null.


Comments