'Razor pages can't bind enum to dropdown
I have this dropdown on a razor page that's inside an Area. I've tried many options and the result is always an empty input. This is the most commonly suggested solution on stackoverflow:
<div class="form-group">
<label asp-for="Transaction.TransactionType" class="control-label"></label>
<select asp-for="Transaction.TransactionType" asp-items="Html.GetEnumSelectList<TransactionType>()" class="form-control" />
<span asp-validation-for="Transaction.TransactionType" class="text-danger"></span>
</div>
I thought the problem was that the models folder was in the root folder of the application but it doesn't work when I try to use the same enum in a form in the root folder. This is my folder structure:
Solution 1:[1]
If the structure of TransactionType looks like this:
public enum TransactionType
{
TransactionType1 = 0,
TransactionType2 = 1,
TransactionType3 = 2
}
Try to use:
<select asp-for="Transaction.TransactionType" class="form-control">
@foreach (var item in Html.GetEnumSelectList<TransactionType>())
{
<option [email protected]>@item.Text</option>
}
</select>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yiyi You |

