我正在构建一个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?
答案 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)