ASP.NET MVC 3.0 TagBuilder在标记后添加Newline

时间:2011-05-04 13:27:46

标签: asp.net-mvc-3

我正在使用班级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\tEnvironment.NewLine种类?

1 个答案:

答案 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);
    ...
}