tagbuilder没有出现在源代码中

时间:2013-05-17 00:25:40

标签: asp.net-mvc html-helper tagbuilder

我开始使用Asp.Net Mvc Html helper处理tagbuilder。我正在尝试使用tagbuilder创建一个表格,当我看到源代码时,会创建表格,但不会创建trtd。我错过了什么意思?

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 个答案:

答案 0 :(得分:1)

你错过了将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());
    }