我正在尝试使用一个标签生成器来构建两个unordered lists。
public static MvcHtmlString GenerateMultipleUL<T>(this HtmlHelper html, IGridable<T> model)
where T : class
{
int itemsCount = model.RowModels.Count();
TagBuilder ulTag = new TagBuilder("ul");
foreach(var indexedItem in model.RowModels.Select((p, i)=> new {item = p, Index = i}))
{
if (itemsCount / 2 == indexedItem.Index)
{ //create a new Un ordered List
ulTag = new TagBuilder("ul"); // This resets the old values with new ones but i want to close the old UL and create a new one.
}
TagBuilder liTag = new TagBuilder("li");
..........
ulTag.InnerHtml += liTag;
}
return new MvcHtmlString(ulTag.ToString());
}发布于 2013-05-21 03:05:19
如果我理解你的问题,你应该使用一个单独的StringBuilder来保存生成的超文本标记语言输出。这将为您在继续生成第二个UL之前存储第一个UL的结果提供一个位置。
StringBuilder output = new StringBuilder();
TagBuilder ulTag = new TagBuilder("ul");
foreach (var item in model)
{
if (testCondition(item))
{
output.Append(ulTag.ToString());
ulTag = new TagBuilder("ul");
}
...
}
output.Append(ulTag.ToString();
return output.ToString();https://stackoverflow.com/questions/16656027
复制相似问题