Question

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?

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()
Was it helpful?

Solution

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

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());
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top