c# - Using ASP.NET MVC 5, how do I get the localized DisplayAttribute string for an Enum value in a View? -
if using displayattribute
on enum
value, how localized resource value based on enum
value.
for example, if have enum
defined following:
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, }
and, assume have created .resx
file custom namespace of modelres contains following:
how, retrieve proper localized value if have value of enum? use proper localized value display in view.
i have looked @ solutions use type converters, extension methods, etc., seem add lot of code should relatively straightforward. using displayattribute
other purposes makes localization pretty trivial handles internally.
the simplest way found solve problem not require code following code in view itself:
@modelres.expiremode.resourcemanager.getstring(model.expiremode.tostring())
model.expiremode
property on model of enum
type expiremode
. modelres
custom namespace resource class expiremode
. grab resourcemanager
, call getstring
string representation of enum value, gives me localized value.
Comments
Post a Comment