c# .net core TagBuilder It doesn't show the tag part

685 views Asked by At

I want to print my table in the infinite category system to my cshtml screen with tagbuilder. writes some of it. some of it is not written. please help me.

c# codes:

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 codes:

@model TagBuilder

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

@Model

screenshot:

enter image description here

console output:

enter image description here

In the console, the sub-categories should normally be listed in the section that says "Microsoft.AspNetCore.Mvc.Rendering.TagBuilder" inside the li tag. but it's not working.

1

There are 1 answers

0
Yiyi You On

You need to convert TagBuilder.InnerHtml to string,if you want to append it to 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);
        }