Get DisplayName Attribute without using LabelFor Helper in asp.net MVC -


what best way retrieve display name attribute item in model? see lot of people using labelfor helper everything, label isn't appropriate if want list data out. there easy way name attribute if want print out in, paragraph?

<p>     <%= html.encode(         modelmetadata.fromlambdaexpression<yourviewmodel, string>(             x => x.someproperty, viewdata).displayname     ) %> <p> 

obviously in order avoid spaghetti code idea write helper:

public static class htmlextensions {     public static mvchtmlstring getdisplayname<tmodel, tproperty>(         htmlhelper<tmodel> htmlhelper,          expression<func<tmodel, tproperty>> expression     )     {         var metadata = modelmetadata.fromlambdaexpression<tmodel, tproperty>(expression, htmlhelper.viewdata);         string value = metadata.displayname ?? (metadata.propertyname ?? expressionhelper.getexpressiontext(expression));         return mvchtmlstring.create(value);     } } 

and then:

<p>     <%: html.getdisplayname(x => x.someproperty) %> </p> 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -