将字符串生成器转换为tagbuilder asp.net mvc 4

时间:2016-06-23 10:33:08

标签: asp.net-mvc tagbuilder

我正在构建一个HTML Helper并完成这项工作,但我使用StringBuilder类来输出以下内容

<div class="panel panel-default">
    <div class="panel-heading">The Panel Title</div>
    <div class="panel-body">
        <h2><span>Content stuff</span></h2>
    </div>
</div>

如何使用TagBuilder类编写MVC 4 HTML Helper?

2 个答案:

答案 0 :(得分:1)

TagBuilder span = new TagBuilder("span");
span.InnerHtml = "Content stuff";

TagBuilder heading = new TagBuilder ("h2");
heading.InnerHtml = span.ToString();

StringBuilder html = new StringBuilder();

TagBuilder div = new TagBuilder("div");
div.AddCssClass("panel-heading");
div.InnerHtml = "The Panel Title";
html.Append(div.ToString());

div = new TagBuilder("div");
div.AddCssClass("panel-body");
div.InnerHtml = heading.ToString();
html.Append(div.ToString());

div = new TagBuilder("div");
div.AddCssClass("panel panel-default");
div.InnerHtml = html.ToString();

return MvcHtmlString.Create(div.ToString());

答案 1 :(得分:0)

我希望它会帮助别人。我为package.json编写了extension来实现复杂的html结构:

npm install

并产生以下结果:

TagBuilder