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

silverlight - Applying a style to ItemContainerStyle in C# -

c# - NullReferenceException in MySqlClient.NativeDriver -

php - How can I merge Nodes & Webform Submissions into instances of one general Content Type in Drupal 6? -