I have the 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)
{
// some other stuff
var viewModel = new MyViewModel { SearchTerm = search };
return View(viewModel);
}
@Html.EnumDropDownListFor(m => m.EmploymentType, "", new { @class = "form-control" })
When I load the page the dropdown default to the first item FullTime
instead of the empty option. I am not setting the default value in my controller so why does it default to the first item and how can i get it to default to the empty option value instead?
The solution for this is to make the model enum property a nullable type:
public class MyViewModel
{
public string searchTerm { get; set; }
public EmploymentType? EmploymentType { get; set; }
}
This way the property allows for null entries and by default it will be null.