Skip to content

Instantly share code, notes, and snippets.

@vanillajonathan
Created November 16, 2017 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanillajonathan/1f6ae7fb43be41a8c5277b9f5c514192 to your computer and use it in GitHub Desktop.
Save vanillajonathan/1f6ae7fb43be41a8c5277b9f5c514192 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC TagHelper for description using the Display attribute
using System;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace WebApplication.TagHelpers
{
/// <summary>
/// <see cref="ITagHelper"/> implementation targeting &lt;span&gt; elements with an <c>asp-description-for</c> attribute.
/// </summary>
[HtmlTargetElement("span", Attributes = AttributeName)]
public class DescriptionTagHelper : TagHelper
{
private const string AttributeName = "asp-description-for";
/// <summary>
/// An expression to be evaluated against the current model.
/// </summary>
[HtmlAttributeName(AttributeName)]
public ModelExpression For { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
if (!output.IsContentModified)
{
output.Content.SetContent(For.Metadata.Description);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment