嵌套foreach以在行上显示

本文关键字:显示 foreach 嵌套 | 更新日期: 2023-09-27 18:25:00

在C#中,我正在尝试使用嵌套的foreach来显示文章列表。我得到的平面HTML要求每3篇文章有一个<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>
        }

我希望每3篇文章都包含div class="row"。我已经说到了这一点,但它在第一个foreach中的"数据"上进行了"spazzing"。

   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++;
}

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

嵌套foreach以在行上显示

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

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

u在这方面太复杂了
试试这个:

        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>