在 asp.net core 上本地化枚举条目 [英] Localizing Enum entry on asp.net core
本文介绍了在 asp.net core 上本地化枚举条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 asp.net core 上本地化以下枚举条目?我在 asp.net-core github 存储库(
解决方案
这似乎是一个 bug,将在 3.0.0 中修复:https://github.com/aspnet/Mvc/issues/7748
服务器端的解决方法是这样的:
私有列表GetPhoneStateEnumList(){var list = new List();foreach(Enum.GetValues(typeof(PhoneState))中的PhoneState项){list.Add(new SelectListItem{Text = Enum.GetName(typeof(PhoneState), item),值 = item.ToString()});}返回 list.OrderBy(x => x.Text).ToList();}
How can I localize the following enum entries on asp.net core? I found few issues on asp.net-core github repository ( https://github.com/aspnet/Mvc/pull/5185 ), but I can't find a proper way to do it.
Target enum:
public enum TestEnum
{
[Display(Name = "VALUE1_RESX_ENTRY_KEY")]
Value1,
[Display(Name = "VALUE3_RESX_ENTRY_KEY")]
Value2
}
CSHTML code block:
<select id="test" asp-items="Html.GetEnumSelectList<TestEnum>()">
</select>
Resource files:
解决方案
It seems to be a bug which will be fixed in 3.0.0: https://github.com/aspnet/Mvc/issues/7748
A server side workaround would be something like this:
private List<SelectListItem> GetPhoneStateEnumList()
{
var list = new List<SelectListItem>();
foreach (PhoneState item in Enum.GetValues(typeof(PhoneState)))
{
list.Add(new SelectListItem
{
Text = Enum.GetName(typeof(PhoneState), item),
Value = item.ToString()
});
}
return list.OrderBy(x => x.Text).ToList();
}
这篇关于在 asp.net core 上本地化枚举条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文