0

在 C# 中,我试图做一个嵌套foreach来显示文章列表。我得到的平面 HTML<div class="row">每 3 篇文章需要一个。

我简单地显示文章的原始代码如下所示:

@foreach (var article in Model)
        {

            <div class="spanthird col">
                <div class="well">
                    <article>
                        <h3>@article.UrlStringDisplay</h3>
                        <div class="date">@article.Date.ToString("dd MMMM yyyy")</div>
                        <a href="@Url.RouteUrl("PressReleases", new {id = article.UrlString, Action = "article", Controller = "PressReleases"})" class="btn btn-primary">Read more</a>
                    </article>
                </div>
            </div>
        }

我想为每 3 篇文章添加 div class="row" 。我已经到了这一点,但它在第一个中的“数据”上“令人震惊” foreach

   int i = 0;
foreach (var row in )
{
    if (i % 3 == 0) {
        @:<div class="row">
    }

    foreach (var article in Model)
        {

            <div class="spanthird col">
                <div class="well">
                    <article>
                        <h3>@article.UrlStringDisplay</h3>
                        <div class="date">@article.Date.ToString("dd MMMM yyyy")</div>
                        <a href="@Url.RouteUrl("PressReleases", new {id = article.UrlString, Action = "article", Controller = "PressReleases"})" class="btn btn-primary">Read more</a>
                    </article>
                </div>
            </div>
        }

    if (i % 3 == 2 || i == data.Count() - 1)
    {
        @:</div>
    }
    i++;
}

我快到了。我只需要朝着正确的方向轻推。

4

2 回答 2

3

这是我能想到的最干净的:

@for (int i = 0; i < Model.Count; i+=3)
{
    <div class="row">
        @foreach (var article in Model.Skip(i).Take(3))
        {
            <div class="spanthird col">
                <div class="well">
                    <article>
                        <h3>@article.UrlStringDisplay</h3>
                        <div class="date">@article.Date.ToString("dd MMMM yyyy")</div>
                        <a href="@Url.RouteUrl("PressReleases", new {id = article.UrlString, Action = "article", Controller = "PressReleases"})" class="btn btn-primary">Read more</a>
                    </article>
                </div>
            </div>                
        }
    </div>
}
于 2013-07-26T13:57:28.523 回答
0

你太复杂了
试试这个:

        int row = 1;
        @foreach (var article in Model)
        {
            @if(row == 1)
                @:<div class="row">
            <div class="spanthird col">
                <div class="well">
                    <article>
                        <h3>@article.UrlStringDisplay</h3>
                        <div class="date">@article.Date.ToString("dd MMMM yyyy")</div>
                        <a href="@Url.RouteUrl("PressReleases", new {id = article.UrlString, Action = "article", Controller = "PressReleases"})" class="btn btn-primary">Read more</a>
                    </article>
                </div>
            </div>
            @if(row == 3)
                @:</div>
            <% row++; %>
            @if(row == 4)
                <% row = 1; %>
        }
        @if(i != 1)
             @:</div>
于 2013-07-26T13:54:28.567 回答