Skip to content

Instantly share code, notes, and snippets.

Last active July 5, 2022 20:21
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Outputs the DataAttribute [Display(Description="long desc")] Has the option to override the tag (defaults to span) and option to add a cssClassName Usage inline in comments
public static class HtmlHelpers
/// Code started from SO:
/// <summary>
/// Usage:
/// @Html.DisplayFor(m => m.PropertyName)
/// supply cssclass name, and override span with div tag
/// @Html.DisplayFor(m => m.PropertyName, "desc", "div")
/// using the named param
/// @Html.DisplayFor(m => m.PropertyName, tagName: "div")
/// </summary>
public static MvcHtmlString DescriptionFor<TModel, TValue>(this HtmlHelper<TModel> self,
Expression<Func<TModel, TValue>> expression,
string cssClassName = "", string tagName = "span")
var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
var description = metadata.Description;
if (!string.IsNullOrEmpty(description))
var tag = new TagBuilder(tagName) {InnerHtml = description};
if (!string.IsNullOrEmpty(cssClassName))
return new MvcHtmlString(tag.ToString());
return MvcHtmlString.Empty;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment