[英]Razor pages can't bind enum to dropdown
I have this dropdown on a razor page that's inside an Area.我在一个区域内的 razor 页面上有这个下拉菜单。 I've tried many options and the result is always an empty input.我尝试了很多选项,结果始终是空输入。 This is the most commonly suggested solution on stackoverflow:这是 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:如果TransactionType
的结构如下所示:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.