简体   繁体   中英

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:

在此处输入图像描述

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 value=@item.Value>@item.Text</option>
        }
    </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM