tagbuilder没有出现在源代码中

时间:2023-01-17 22:33:44

I started working on Asp.Net Mvc Html helper with tagbuilder. I am trying to create a table with tagbuilder and when I see source, table is created but not tr and td. Where I am missing the point?

我开始使用tagbuilder处理Asp.Net Mvc Html助手。我正在尝试使用tagbuilder创建一个表,当我看到source时,表创建但不是tr和td。哪里我错过了重点?

public static HtmlString TbTile(this HtmlHelper helper)
            {
                TagBuilder tagBuilder = new TagBuilder("table");
                tagBuilder.Attributes["style"] = "background-color:red;height:200px;";
                tagBuilder.Attributes["width"] = "100%";
                tagBuilder.Attributes["border"] = "1";
                TagBuilder tbody = new TagBuilder("tbody");
                TagBuilder tr = new TagBuilder("tr");
                TagBuilder td = new TagBuilder("td");
                TagBuilder th = new TagBuilder("th");
                td.Attributes["style"] = "background-color:green;height:100px;";
                td.Attributes["height"] = "50%";
                td.InnerHtml = "test td1";
                return new HtmlString(tagBuilder.ToString());
            }


@Html.TbTile()

1 个解决方案

#1


1  

You missed the step which you should put td intu tr and tr into table. Try this:

你错过了将td intu tr和tr放入表中的步骤。尝试这个:

public static HtmlString TbTile(this HtmlHelper helper)
    {
        TagBuilder tagBuilder = new TagBuilder("table");
        tagBuilder.Attributes["style"] = "background-color:red;height:200px;";
        tagBuilder.Attributes["width"] = "100%";
        tagBuilder.Attributes["border"] = "1";
        TagBuilder tbody = new TagBuilder("tbody");
        TagBuilder tr = new TagBuilder("tr");
        TagBuilder td = new TagBuilder("td");
        TagBuilder th = new TagBuilder("th");
        td.Attributes["style"] = "background-color:green;height:100px;";
        td.Attributes["height"] = "50%";
        td.InnerHtml = "test td1";
        /*missed*/
        tr.InnerHtml = td.ToString();
        tagBuilder.InnerHtml = tr.ToString();
        /**/
        return new HtmlString(tagBuilder.ToString());
    }

#1


1  

You missed the step which you should put td intu tr and tr into table. Try this:

你错过了将td intu tr和tr放入表中的步骤。尝试这个:

public static HtmlString TbTile(this HtmlHelper helper)
    {
        TagBuilder tagBuilder = new TagBuilder("table");
        tagBuilder.Attributes["style"] = "background-color:red;height:200px;";
        tagBuilder.Attributes["width"] = "100%";
        tagBuilder.Attributes["border"] = "1";
        TagBuilder tbody = new TagBuilder("tbody");
        TagBuilder tr = new TagBuilder("tr");
        TagBuilder td = new TagBuilder("td");
        TagBuilder th = new TagBuilder("th");
        td.Attributes["style"] = "background-color:green;height:100px;";
        td.Attributes["height"] = "50%";
        td.InnerHtml = "test td1";
        /*missed*/
        tr.InnerHtml = td.ToString();
        tagBuilder.InnerHtml = tr.ToString();
        /**/
        return new HtmlString(tagBuilder.ToString());
    }