将 SelectTagHelper 绑定到枚举,对 SelectList 使用 Display(Name)(但保留字符串值而不是 int)
Binding a SelectTagHelper to an Enum, using Display(Name) for SelectList (yet persisting string Value instead of int)
(此处为 WPF 开发人员,网络应用新手)
在我的数据库中,我有一个包含字符串的字段。为简单起见,我们假设字符串表示国家/地区。 字符串包含空格(例如"United States")。
我需要提供一种方法来更新给定记录的该字段。
我计划使用 EnumMemberAttribute [EnumMember(Value = "United States")]
将字符串而不是 int 保存到数据库(以防止在添加枚举项时出现问题,并促进可读性——也因为新的带字符串的记录将定期批量插入,需要强制转换才能存储为整数,并且将来可能包括其他选项/拼写错误。
我做了一个枚举,但我无法让我的 SelectTagHelper select 任何给定记录的正确项目。 第一项 ("") selected 而不是。
public enum CountryEnum
{
[Display(Name="")]
None,
[Display(Name = "United States of America")]
USA,
[Display(Name = "Great Britain")]
UK
}
我的 SelectTagHelper:
<select asp-for="SelectedRecord.Country" asp-items="Html.GetEnumSelectList<MyApp.Models.CountryEnum>()">
我的错误:我在 class 中使用了枚举,但没有这样声明它:属性 是一个字符串,而不是我的枚举(的一个实例)。
public class Employee
{
public string Country {get; set;}
}
(士气:生病时不要编码)
(此处为 WPF 开发人员,网络应用新手)
在我的数据库中,我有一个包含字符串的字段。为简单起见,我们假设字符串表示国家/地区。 字符串包含空格(例如"United States")。
我需要提供一种方法来更新给定记录的该字段。
我计划使用 EnumMemberAttribute [EnumMember(Value = "United States")]
将字符串而不是 int 保存到数据库(以防止在添加枚举项时出现问题,并促进可读性——也因为新的带字符串的记录将定期批量插入,需要强制转换才能存储为整数,并且将来可能包括其他选项/拼写错误。
我做了一个枚举,但我无法让我的 SelectTagHelper select 任何给定记录的正确项目。 第一项 ("") selected 而不是。
public enum CountryEnum
{
[Display(Name="")]
None,
[Display(Name = "United States of America")]
USA,
[Display(Name = "Great Britain")]
UK
}
我的 SelectTagHelper:
<select asp-for="SelectedRecord.Country" asp-items="Html.GetEnumSelectList<MyApp.Models.CountryEnum>()">
我的错误:我在 class 中使用了枚举,但没有这样声明它:属性 是一个字符串,而不是我的枚举(的一个实例)。
public class Employee
{
public string Country {get; set;}
}
(士气:生病时不要编码)