addTagHelper Html.GetEnumSelectList Not Working

Viewed 1522

Just getting back into coding after a long hiatus... I'm trying to display a list from an enum using Html.GetEnumSelectList<>, however Visual Studio 2019 does not seem to recognize (no intellisense) and the code is not working.

@using AspNetCoreVideo.Models
@model AspNetCoreVideo.Entities.Video
@addTagHelper *, Microsoft.aspnetcore.mvc.taghelpers

<h2>Create Vidio</h2>
<form asp-action="Create" method="post">

<table>
    <tr>
        <td><label asp-for="Title"></label></td>
        <td><input asp-for="Title" /></td>
    </tr>
    <tr>
        <td><label asp-for="Genre"></label></td>
    <    td><select asp-for="Genre" asp-items="Html.GetEnumSelectList<Genres>()"></select></td>
    </tr>
</table>

<input type="submit" value="Create" />

</form>

Expecting a list of "Genres" to display, however, nothing is in the list.

3 Answers

I also had the similar issue, the select was showing nothing. Later I found that it was because of the usage of the select tag. If you use it as a single element like this <select /> it isn't gonna work. You have to use it as <select></select> Hope this would help someone.

Issue Resolved. Apparently, @addTagHelper directive is case sensitive. I changed "@addTagHelper *, Microsoft.aspnetcore.mvc.taghelpers" to "@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers" and now the tag helpers work fine.

The reason you're not getting intellisense is because you didn't prefix it with an @, so it's just plain text. You only don't need the @ on asp-for. That's because it's accepting a model expression, whereas something like asp-items needs an actual instance of some collection. In other words:

<select asp-for="Genre" asp-items="@Html.GetEnumSelectList<Genres>()"></select>
Related