我开始使用Asp.Net Mvc Html helper
处理tagbuilder
。我正在尝试使用tagbuilder
创建一个表格,当我看到源代码时,会创建表格,但不会创建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()
答案 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());
}