简体   繁体   English

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

[英]How can I make an unordered list appear within a foreach loop in MVC3 Razor?

I have the following code: 我有以下代码:

@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>
    }
}

My input data (value of topic.RowKey) looks like this: 我的输入数据(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....

This works but what I would really like to do is to have an h3 heading every time the first two digits of the RowKey change and then between then and then next h3 heading I would like to have an unordered list instead of just <p>xxx</p> . 这有效,但我真正想做的是每次RowKey的前两位数改变时都有一个h3标题,然后在那之间再到下一个h3标题我希望有一个无序列表而不仅仅是<p>xxx</p> I have tried lots of different combinations of things but nothing works. 我尝试了许多不同的组合,但没有任何作用。 Is this even possible to do with Razor? 这甚至可以用剃刀吗? Where I had huge problems was how can I get the <ul> and </ul> s to appear correctly? 我遇到的大问题是如何才能正确显示<ul></ul> I know I can put a <ul> after the <h3> but how can I place the </ul> ? 我知道我可以在<h3> <ul>之后添加<ul>但是如何放置</ul>

not checked in razor, there may be an @ missing here or there, but... 未经剃须刀检查,可能会有@在这里或那里失踪,但......

@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>

}
    @{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>

It would be more easy create a model parent-child, populated before send it to the view 创建一个模型父子会更容易,在将其发送到视图之前填充

the model 该模型

public class topic {

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

    }

the view 风景

@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>
    }
}

Its not about razor, this is something you can solve with any rendering enginge, web or windows forms or anything else. 它不是剃须刀,这是你可以解决任何渲染引擎,网页或窗体或其他任何东西。

@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>  
    }
}
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM