asp.net mvc - Get [Display] attribute value from resource in MVC -
i have used [display] attribute 1 of enums:
public enum ecommenttype { [display(resourcetype = typeof(faultmanagementstrings), name = "normalcomment")] normalcomment = 0, [display(resourcetype = typeof(faultmanagementstrings), name = "openningcomment")] openningcomment = 1, [display(resourcetype = typeof(faultmanagementstrings), name = "startprogresscomment")] startprogresscomment = 2, [display(resourcetype = typeof(faultmanagementstrings), name = "clousercomment")] clousercomment = 3, [display(resourcetype = typeof(faultmanagementstrings), name = "reopennigncomment")] reopennigncomment = 4 } is possible create extention method reuse exsisting mvc finctionallity of getting display attribute value specified resource ?
i that...
@html.getenumdisplayattributevalue(c=> comment.commenttype) i know wirte implement required reflection , find value of resource type , call resource manager , on... think maybe possible use exsisting built in functionally of mvc.. after done when call labelfor helper.
is possible or should reinvent wheel ?
i have had same problem , created these extension methods:
using system; using system.componentmodel.dataannotations; using system.linq; using system.linq.expressions; using system.web.mvc; using system.web.mvc.html; public static class enumhelper { private static readonly selectlistitem[] singleemptyitem = new[] { new selectlistitem { text = string.empty, value = string.empty } }; public static mvchtmlstring enumdropdownlistfor<tmodel, tenum>(this htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tenum>> expression) { return htmlhelper.enumdropdownlistfor(expression, null); } public static mvchtmlstring enumdropdownlistfor<tmodel, tenum>(this htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tenum>> expression, object htmlattributes) { var metadata = modelmetadata.fromlambdaexpression(expression, htmlhelper.viewdata); var enumtype = getnonnullablemodeltype(metadata); var values = enum.getvalues(enumtype).cast<tenum>(); var items = values.select(value => new selectlistitem { text = getname(value), value = value.tostring(), selected = value.equals(metadata.model) }); if (metadata.isnullablevaluetype) { items = singleemptyitem.concat(items); } return htmlhelper.dropdownlistfor(expression, items, htmlattributes); } private static string getname<tenum>(tenum value) { var displayattribute = getdisplayattribute(value); return displayattribute == null ? value.tostring() : displayattribute.getname(); } private static displayattribute getdisplayattribute<tenum>(tenum value) { return value.gettype() .getfield(value.tostring()) .getcustomattributes(typeof(displayattribute), false) .cast<displayattribute>() .firstordefault(); } private static type getnonnullablemodeltype(modelmetadata modelmetadata) { var realmodeltype = modelmetadata.modeltype; var underlyingtype = nullable.getunderlyingtype(realmodeltype); return underlyingtype ?? realmodeltype; } } you can use these extension methods in views follows:
@html.enumdropdownlistfor(c=> comment.commenttype) you should see dropdownlist containing enum values' names according displayattribute.
the actual retrieval of displayed enum value done in getname method, first uses getdisplayattribute method try retrieve displayattribute of enum value. if enum value indeed decorated displayattribute, use retrieve name displayed. if there no displayattribute, return name of enum value.
Comments
Post a Comment