<p asp-for-display="item.Article" length="400"></p> (Substring(0, 400))
<div asp-for-display="item.Article" length="160"></div> (Substring(0, 160))
<span asp-for-display="item.Article"></span> (Full length)
<h1 asp-for-display="item.Article"></h1> (Full length)
**<b asp-for-display="item.Article"></b>** (Not work, HtmlTargetElement not exist)
Asp.Net Core TagHelper
/// <summary>
/// <see cref="ITagHelper"/> implementation targeting <p> elements with an <c>asp-for</c> attribute.
/// </summary>
[HtmlTargetElement("p", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("div", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("dd", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("span", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("th", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("td", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("span", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("i", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h1", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h2", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h3", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h4", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h5", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("h6", Attributes = ModelValueForAttributeName)]
[HtmlTargetElement("code", Attributes = ModelValueForAttributeName)]
public class ModelValueForTagHelper : TagHelper
{
private const string ModelValueForAttributeName = "asp-for-display";
[HtmlAttributeName(ModelValueForAttributeName)]
public ModelExpression For { get; set; }
public int length { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
***// If you need clear result use "TagName = "";"***
//output.TagName = "label";
//output.TagMode = TagMode.StartTagAndEndTag;
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (output == null)
{
throw new ArgumentNullException(nameof(output));
}
var text = For.ModelExplorer.GetSimpleDisplayText();
if (text.Length >= length)
{
text = text.Substring(0, length) + "...";
}
output.Content.SetContent(text);
}
}