1

我开始Asp.Net Mvc Html helper使用tagbuilder. 我正在尝试使用 and 创建一个表,tagbuilder当我看到源时,表已创建但不是trand 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()
4

1 回答 1

2

您错过了应该将 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());
    }
于 2013-05-17T00:43:04.980 回答