If I am understanding your question, you should be using a separate StringBuilder
to hold the generated HTML for output. This will give you a place to store the results of your first UL before continuing with generating the second.
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();