Created
November 16, 2017 14:18
-
-
Save vanillajonathan/1f6ae7fb43be41a8c5277b9f5c514192 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC TagHelper for description using the Display attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Mvc.ViewFeatures; | |
using Microsoft.AspNetCore.Razor.TagHelpers; | |
namespace WebApplication.TagHelpers | |
{ | |
/// <summary> | |
/// <see cref="ITagHelper"/> implementation targeting <span> 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