.NET logo

How to get the Description for a property in ASP.NET MVC 5?

This post was most recently updated on March 28th, 2023.

2 min read.

This post describes the easiest way to show description texts for properties in ASP.NET MVC 5. Sounds simple, right? We can use something like @Html.DescriptionFor(x => x.Property), right? Alas, it’s not that straightforward.

A bit surprisingly, there’s no ready-made helper function for this. I’m saying “surprisingly”, because I feel like MVC 5 really already has all the bells and whistles – AND the kitchen sink. But no, it does not have a helper for getting a description of a property :)

You can, however, define the Description using the Data Annotations in ASP.NET.

[Display(
 Name = "Your Property",
 Description = "This is your property which you can use for whatever."
)]
[Required]
public string YourProperty{ get; set; }

With that, we just need to figure out a way to access the value of the Description. But how do we do that?

Solution

Even though there’s no built-in extension, we can always write one. For that, we’ll define a new class that extends the HtmlHelper class.

See below for a code example:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace YourAssembly.Html 
{
 public static class HtmlHelperExtensions 
 {
  /// 
  /// Get a description for a property.
  /// 
  /// Adapted from: https://stackoverflow.com/questions/26967329/using-a-htmlhelper-in-mvc-5
  /// With this as the model for extracting description: https://stackoverflow.com/questions/6578495/how-do-i-display-the-displayattribute-description-attribute-value
  /// Based on these annotations on the model: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.description%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
  /// 
  /// 
  /// 
  /// The HtmlHelper (called with @Html on the views)
  /// Lambda for the property that we'll show the description for. For example model => model.Created
  /// The description of the property, if available. Alternatively, will return the DisplayName or PropertyName (internal name).
  public static MvcHtmlString DescriptionFor < TModel, TValue > (this HtmlHelper < TModel > helper, Expression < Func < TModel, TValue >> expression) 
  {
   var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
   var description = metadata.Description;

   // fallback! We'll try to show something anyway.
   if (String.IsNullOrEmpty(description)) description = String.IsNullOrEmpty(metadata.DisplayName) ? metadata.PropertyName : metadata.DisplayName;

   return MvcHtmlString.Create(description);
  }
 }
}

Okay, so now we have an extension method called DescriptionFor(). But how to use this?

Luckily, since we’re extending the HtmlHelpers directly, as long as you’re including the class your extension is in, you can use it exactly like you’d use any other extension method!

See below for an example of how to use this in a View – just like you use built-in methods such as ValidationMessageFor():

@using YourAssembly.Html


   
@Html.LabelFor(model => model.YourProperty, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.YourProperty, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.YourProperty, "", new { @class = "text-danger" }) @Html.DescriptionFor(model => model.YourProperty)

And with this, you should be good!


The method itself might look a bit convoluted, but at least it’s easy to use afterwards. Give it a try and let me know how it goes! :)

mm
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments