本文介绍了在ASP.NET Core上本地化Enum条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在asp核心上本地化以下枚举条目?我在asp核心github存储库上发现了几个问题(
解决方案似乎是一个错误,将在3.0.0中修复: github/aspnet/Mvc/issues/7748
服务器端的解决方法是这样的:
私有列表< SelectListItem>GetPhoneStateEnumList(){var list = new List< SelectListItem>();foreach(Enum.GetValues(typeof(PhoneState))中的PhoneState项){list.Add(new SelectListItem{文字= Enum.GetName(typeof(PhoneState),item),值= item.ToString()});}返回list.OrderBy(x => x.Text).ToList();}How can I localize the following enum entries on asp core? I found few issues on asp-core github repository ( github/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: github/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上本地化Enum条目
发布评论