Get DisplayName Attribute without using LabelFor Helper in asp.net MVC
问题 What is the best way to retrieve the display name attribute for an item in your model? I see a lot of people using the LabelFor helper for everything, but a label isn't appropriate if I just want to list the data out. Is there an easy way just get the Name Attribute if I just want to print it out in, say a paragraph? 回答1: <p> <%= Html.Encode( ModelMetadata.FromLambdaExpression<YourViewModel, string>( x => x.SomeProperty, ViewData).DisplayName ) %> <p> Obviously in order to avoid the spaghetti