Using enum for dropdown list in ASP.NET MVC Core
I think you accidentally used GenderType
instead of Gender
. The correct syntax is
<select asp-for="GenderType" asp-items="Html.GetEnumSelectList<Gender>()"> <option selected="selected" value="">Please select</option></select>
GenderType
is your property name, not the Enum type. The GetEnumSelectList method expects you to give it the type of the Enumeration, not the name of the property in your model.
Try this:
Html.GetEnumSelectList<Gender>()
You can simply use Razor syntax:
@Html.DropDownList("StudentGender", Html.GetEnumSelectList<Gender>(), "Select Gender",new { @class = "form-control" })