在ASP.NET Core上本地化Enum条目

编程入门 行业动态 更新时间:2024-10-24 06:38:55
本文介绍了在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条目

本文发布于:2023-11-15 06:34:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   NET   ASP   Enum   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!