簡體   English   中英

在Asp.Net core 1.0(Visual Studio 2015)中的標簽幫助器中需要幫助

[英]Need help in tag helper in Asp.Net core 1.0 (Visual Studio 2015)

我無法使標記幫助程序在Visual Studio 2015中運行ASP.Net核心項目。該項目中沒有編譯錯誤。 當我運行項目時,標記幫助程序未呈現。 我將斷點放在Tag helper的“ Process”方法中,但它並未到達那里。 請讓我知道如何使其工作。

1.在“ project.json”文件的“依賴項”部分中包含“ Microsoft.AspNet.Mvc.TagHelpers”:“ 6.0.0-rc1-final”。

2.在_ViewImports.cshtml文件中添加了@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers。

3,在Index.cshtml中添加以下代碼

<table generate-rows="@Model.Count()" source-model="@Model"></table>

4,標簽助手代碼

using System.Collections;
using System.Text;
using System.Reflection;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace MVC_TagHelper.CustomTagHelper
{
    [HtmlTargetElement("table",Attributes ="generate-rows,source-model")]
    public class TableTagHelper : TagHelper
    {
        [HtmlAttributeName("generate-rows")]
        public int RepeatCount { get; set; }
        [HtmlAttributeName("source-model")]
        public ModelExpression DataModel { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            IEnumerable model = DataModel.Model as IEnumerable;
            if (model == null)
                return;
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (var m in model)
                {
                    PropertyInfo[] properties = m.GetType().GetProperties();
                    string html = "<tr>";
                    for (int i = 0; i < properties.Length; i++)
                    {
                        html += "<td>" + m.GetType().GetProperty(properties[i].Name).GetValue(m, null) + "</td>";
                    }
                    html += "</tr>";
                    sb.Append(html);
                }
                output.Content.SetHtmlContent(sb.ToString());
            }
        }
    }
}

檢查_ViewImports.cshtml 確保它包含對標記幫助器類名稱空間的引用:

@using MVC_TagHelper;
@addTagHelper *, MVC_TagHelper

我不久前也遇到了同樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM