【发布时间】:2015-07-18 05:51:53
【问题描述】:
如果我在Enum 值上使用DisplayAttribute,如何根据Enum 值获取本地化资源值。
例如,如果我有一个 Enum 定义如下:
public enum ExpireMode {
[Display(ResourceType = typeof (ModelRes.ExpireMode), Name = "Never")]
Never = 0,
[Display(ResourceType = typeof (ModelRes.ExpireMode), Name = "ByCreated")]
ByCreated = 1,
[Display(ResourceType = typeof (ModelRes.ExpireMode), Name = "ByLastAccessed")]
ByLastAccessed = 2,
}
并且,我们假设我已经创建了一个 .resx 文件,其中包含以下内容:
如果我只有枚举的值,我将如何检索正确的本地化值?我想使用正确的本地化值在视图中显示。
我看过使用类型转换器、扩展方法等的解决方案,但似乎都添加了很多应该相对简单的代码。将DisplayAttribute 用于所有其他目的使得本地化变得非常简单,因为它在内部处理它。
【问题讨论】:
标签: c# asp.net asp.net-mvc enums localization