-1

我想使用 tagbuilder 将无限类别系统中的表格打印到我的 cshtml 屏幕。写了一些。有的没写。请帮我。

c#代码:

public IActionResult Index()
        {
            var tb = new TagBuilder("ul");
            using (SqlDbContext vt = new SqlDbContext())
            {
                
                var Kategori = from i in vt.urunKategorileri
                               where i.urunKategorileriUstId == 0
                               select i;

                foreach (urunKategorileri anamenu in Kategori)
                {
                    tb.InnerHtml.AppendHtml("<li><a href='#'>" + anamenu.urunKategorileriKategoriAdi + "</a>"+ Altkategori(anamenu.urunKategorileriId) + "</li>");
                }

            }
                return View(tb);
        }


        private TagBuilder Altkategori(int id)
        {
             TagBuilder eb = new TagBuilder("ul");
            using (SqlDbContext a = new SqlDbContext())
            {
               
                var say = (from i in a.urunKategorileri
                           where i.urunKategorileriUstId == id
                           select i).Count();
                if (say > 0)
                {
                    TagBuilder ab = new TagBuilder("<ul>");
                    var altKat = from i in a.urunKategorileri
                                 where i.urunKategorileriUstId == id
                                 select i;

                   
                    foreach (urunKategorileri altkategori in altKat)
                    {
                        ab.InnerHtml.AppendHtml("<li><a href='#'>" + altkategori.urunKategorileriKategoriAdi + "</a>"+ Altkategori(altkategori.urunKategorileriId)+ "</li>");
                    }
                  
                    return ab;
                }
            }

            return eb;
        }

cshtml代码:

@model TagBuilder

@{
    ViewData["Title"] = "Home Page";
}

@Model

截屏:

在此处输入图像描述

控制台输出:

在此处输入图像描述

在控制台中,子类别通常应列在 li 标签内的“Microsoft.AspNetCore.Mvc.Rendering.TagBuilder”部分中。但它不起作用。

4

1 回答 1

0

TagBuilder.InnerHtml如果要将其附加到 html,则需要转换为字符串:

public IActionResult Index()
        {
            var tb = new TagBuilder("ul");
            using (SqlDbContext vt = new SqlDbContext())
            {

                var Kategori = from i in vt.urunKategorileri
                               where i.urunKategorileriUstId == 0
                               select i;

                foreach (urunKategorileri anamenu in Kategori)
                {
                    string html = "";
                    using (var writer = new System.IO.StringWriter())
                    {
                        Altkategori(anamenu.urunKategorileriId).InnerHtml.WriteTo(writer, HtmlEncoder.Default);
                        html = writer.ToString();
                    }
                    tb.InnerHtml.AppendHtml("<li><a href='#'>" + anamenu.urunKategorileriKategoriAdi + "</a>" + html + "</li>");
                }

            }
            return View(tb);
        }
于 2021-09-22T06:20:34.343 回答