我正在使用班级ul
TagBuilder
var ulTag = new TagBuilder("ul");
...
foreach(..items...)
{
var liTag = new TagBuilder("li");
....
ulTag.InnerHtml += liTag.ToString();
}
return new MvcHtmlString(ulTag.ToString());
但输出html在一行中产生了
<ul id="menu"><li><a href="/">Home</a></li><li><a href="/About">About</a></li>.....</ul>
但我希望这样:
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/About">About</a></li>
.....
</ul>
有没有办法在每个标记后添加\r\t
或Environment.NewLine
种类?
答案 0 :(得分:5)
不太干净,但可能有效:
var ulTag = new TagBuilder("ul");
ulTag.InnerHTML += Environment.NewLine;
foreach(..items...)
{
var liTag = new TagBuilder("li");
ulTag.InnerHTML += string.Format(" {0}{1}", liTag.ToString(), Environment.NewLine);
...
}