ASP.NET MVC6中的自定义TagHelper& EF 7

时间:2016-02-03 07:35:07

标签: c# asp.net-core-mvc tag-helpers asp.net-core-1.0

这是我的代码:

_ViewImport.cs

@using BFcMVC
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
@addTagHelper "BFcMVC.TagsHelpers.TagsHelpers, BFcMVC"

BFcMVC.TagsHelpers:

using Microsoft.AspNet.Razor.TagHelpers;

namespace BFcMVC.TagsHelpers
{
    public class TagsHelpers : TagHelper
    {
        public static string Truncate(string text, int length)
        {
            if (text.Length >= length)
                return text.Substring(0, length) + "...";

            return text;
        }
    }
}

Index.cshtml

所有下一个代码都返回错误:

@Html.DisplayFor(modelItem => item.Article).Truncate(this, 50)
@Html.DisplayFor(modelItem => item.Article.Truncate(this,50))

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

<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 &lt;p&gt; 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);
    }
}