简体   繁体   English

Razor c#中基于代码的转发器

[英]code based repeater in Razor c#

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

@foreach (var row in Model)
        {
           <div><img src="~/Content/images/flickabase/@row.FlickaMasterImage" alt="@row.FlickaMasterImageCaption" class="img-thumbnail" width="100" heigh="100"/>
            <a href="/flickas/flickaid/@row.Id/flickaname/@row.FlickaName.Trim()">@row.FlickaName</a> </div> 
        }

This prints out a series of thumbnails and boat name.这将打印出一系列缩略图和船名。 Like this.像这样。

单列显示

The problem is that I want to write out four columns with the data running top to bottom, left to right.问题是我想写出四列,数据从上到下,从左到右。 This used to be easy with .net repeater control but I can't see how I can easily do this with Razor.这过去使用 .net 中继器控制很容易,但我看不出如何使用 Razor 轻松做到这一点。 Is there an easy way to do this?是否有捷径可寻? The best idea I have come up with is to count the rows.我想出的最好的主意是计算行数。 Divide by four and then (since I am using Bootstrap) create a grid column for each batch.除以四,然后(因为我使用 Bootstrap)为每个批次创建一个网格列。

Norb.诺布。

thanks for all your comments.谢谢你们的评论。

This is how I solved it in the end.我最后就是这样解决的。 I used Skip and Take on the model to get batches of the data.我在模型上使用 Skip and Take 来获取批量数据。 There is never going to be masses of data and when it grows I'll revisit in the future.永远不会有大量数据,当它增长时,我会在未来重新审视。 I ditched the images in the end.最后我放弃了图片。 The four batches are written out one by one inside a Bootstrap col which does the layout.这四个批次在进行布局的 Bootstrap col 中一一写出。

I am sure there is a more elegant way but this will do for now.我相信有一种更优雅的方式,但现在就可以了。

@{
    Layout = "Shared/_Layout.cshtml";
    ViewBag.Title = "Flicka Names";
    var i = Model.Count(); // count rows
    var size = i/4; // get the batch size

    var first = Model.Take(size);
    var second = Model.Skip(size).Take(size); 
    var third = Model.Skip(size * 2).Take(size);
    var fourth = Model.Skip(size * 3).Take(size);
}

<h1>Flicka Names</h1>
<p>There are @i Flickas in our system.</p>

<div class="col-sm-3">

    @foreach (var row in first)
        {
           <div>
            <a href="/flickas/flickaid/@row.Id/flickaname/@row.FlickaName.Trim()">@row.FlickaName</a> </div>

        }
    </div>

<div class="col-sm-3">
    @foreach (var row in second)
        {
        <div>
            <a href="/flickas/flickaid/@row.Id/flickaname/@row.FlickaName.Trim()">@row.FlickaName</a></div>

        }
</div>


<div class="col-sm-3">
    @foreach (var row in third)
        {
        <div>
            <a href="/flickas/flickaid/@row.Id/flickaname/@row.FlickaName.Trim()">@row.FlickaName</a>
        </div>

    }

</div>

<div class="col-sm-3">
    @foreach (var row in fourth)
            {
        <div>
            <a href="/flickas/flickaid/@row.Id/flickaname/@row.FlickaName.Trim()">@row.FlickaName</a>
        </div>

    }

As you already found out, there is no readymade solution to this in razor ....Bootstrap is the best solution to this as @B.Yaylaci suggested already....please refer to the following question:正如您已经发现的那样, razor没有现成的解决方案....Bootstrap 是解决此问题的最佳解决方案,正如@B.Yaylaci 已经建议的那样....请参考以下问题:

mvc 3 equivalent to <asp:repeater> function? mvc 3 相当于 <asp:repeater> 函数?

Hope it helps, cheers.:)希望它有帮助,干杯。:)

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

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