如何在MVC3 Razor的foreach循环中显示无序列表?

时间:2012-06-08 16:22:15

标签: asp.net-mvc asp.net-mvc-3 razor

我有以下代码:

@foreach (Content topic in Model.Topics)
{
    if (topic.RowKey.Substring(2, 2) == "00")
    {
        <h3>@topic.RowKey.Substring(0, 2).TrimStart('0') - @Html.Raw(topic.Title)</h3>
    }
    else
    {
        <p>@String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - @Html.Raw(topic.Title)</p>
    }
}

我的输入数据(topic.RowKey的值)如下所示:

0100 <- This is topic 1
0101 <- This is topic 1.1
0102 <- This is topic 1.2
0103 <- This is topic 1.3
0200 <- This is topic 2
0201 <- This is topic 2.1
etc....

这样可行,但我真正想做的是每次RowKey的前两位数改变时都有一个h3标题然后在那之间再接下来h3标题我希望有一个无序列表而不是{ {1}}。我尝试了许多不同的组合,但没有任何作用。这甚至可以用剃刀吗?我遇到大问题的地方是如何正确显示<p>xxx</p><ul>?我知道我可以在</ul>之后添加<ul>但是如何放置<h3>

4 个答案:

答案 0 :(得分:8)

没有用剃须刀检查,可能会在这里或那里找不到@,但是......

@var groupedTopics = Model.Topics.GroupBy(t => t.RowKey.Substring(0, 2));

@foreach (var group in groupedTopics) {
  var firstTopic = group.First();
  <h3>@firstTopic.RowKey.Substring(0, 2).TrimStart('0') - @Html.row(firstTopic.Title)</h3>
  <ul>
  @foreach (var topic in group.Skip(1)) {
      <li>@String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - @Html.Raw(topic.Title)</li>
  }
  </ul>

}

答案 1 :(得分:0)

    @{bool isFirst = true;}
    @foreach (Content topic in Model.Topics)
    {
        if (topic.RowKey.Substring(2, 2) == "00")
        {
            if(!isFirst) {
               <text></ul></text>
               isFirst = false
            }
            <h3>@topic.RowKey.Substring(0, 2).TrimStart('0') - @Html.Raw(topic.Title)</h3>
        }
        else
        {   
            <li>
            <p>@String.Format("{0}.{1}",topic.RowKey.Substring(0, 2).TrimStart('0'),topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - @Html.Raw(topic.Title)</p></li>
        }
    }

    </ul>

答案 2 :(得分:0)

创建模型父子会更容易,在将其发送到视图之前填充

模型

public class topic {

        //everything else
        public List<topic> subtopic{ get; set; }

    }

视图

@foreach (Content topic in Model.Topics)
{
    <h3>@topic.RowKey.Substring(0, 2).TrimStart('0') - @Html.Raw(topic.Title)</h3>
    if (topic.subtopic.count > 0)
    {
        <ul>
            @foreach (Content subtopic in topic.subtopic)
            { 
                <li>@String.Format("{0}.{1}", topic.RowKey.Substring(0, 2).TrimStart('0'), topic.RowKey.Substring(2, 2).TrimStart('0').PadLeft(1, '0')) - @Html.Raw(topic.Title)</li>
            }
        </ul>
    }
}

答案 3 :(得分:0)

它不是关于剃须刀,这是你可以用任何渲染引擎,网页或窗体表格或其他任何东西解决的问题。

@model List<Topic>
@{
int currentKey = 0;
bool withinUL = false;
foreach (Topic topic in Model) 
{ 
    if (topic.RowKey != currentKey)   
    {
        currentKey = topic.RowKey;
        if (withinUL)
        {
            withinUL = false;
            @:</ul>
        }


        @:<h3>@topic.RowKey - @Html.Raw(topic.Title)</h3>     

    }   
    else   
    {       
        if (!withinUL)
        {
            withinUL = true;
            @:<ul></ul>
        }
          @:<li>@topic.RowKey - @Html.Raw(topic.Title)</li>  
    }
}
}