获取有关剃须刀的foreach索引值 [英] Getting index value on razor foreach

查看:126
本文介绍了获取有关剃须刀的foreach索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个迭代列表< T> 在我看来,剃刀foreach循环这使得部分。在部分我渲染,我想有4在我看来,一个排的单个记录。我有两个端列的CSS类因此需要在部分来确定呼叫是否是第一或第四次的纪录。什么是确定这在我的部分输出正确的code的最佳方式?

这是包含回路我的主要页面:

  @foreach(在Model.Members VAR myItem){        //若i = 1
        < D​​IV CLASS =grid_20>
        <! - 起始行 - >        //是有什么方法来获得在i = 1到4,并通过对部分?
        @ Html.Partial(nameOfPartial模型)        //若i = 4,则下面的输出和复位我1
        < D​​IV CLASS =清除>< / DIV>
        <! - 结束行 - >
        < / DIV>}

我想我可以创建一个INT,我可以在每次通过更新在这里呈现文本没有问题,但它传递的整数值到我的部分,我更关心的问题。除非有一个更好的办法。

下面是我的部分:

  @ {
开关()
情况1:
        <文字和GT;
        < D​​IV CLASS =grid_4阿尔法>
        < /文字和GT;
打破;
情况4:
        <文字和GT;
        < D​​IV CLASS =grid_4欧米茄>
        < /文字和GT;
打破;
默认:
        <文字和GT;
        < D​​IV CLASS =grid_4>
        < /文字和GT;
打破;
}        < IMG SRC =内容/ 960网/ spacer.gif的风格=WIDTH:130px;高度:160像素;背景色:#FFF;边框:10px的固体#d3d3d3; />
        &所述p为H.;&下; A HREF =构件-card.html> @ Model.Name&下; / A>&所述峰; br />
        @ Model.Job< BR />
        @ Model.Location< / P>
< / DIV>

不知道如果我有一个金发碧眼今天一天,这是可怕容易,但我只是想不出来传递的int值的最佳途径。希望有人能提供帮助。


解决方案

  @ {INT I = 0;}
 @foreach(在Model.Members VAR myItem)
 {
     <跨度> @ I< / SPAN>
     我++;
 }

I'm iterating a List<T> in a razor foreach loop in my view which renders a partial. In the partial I'm rendering a single record for which I want to have 4 in a row in my view. I have a css class for the two end columns so need to determine in the partial whether the call is the 1st or the 4th record. What is the best way of identifying this in my partial to output the correct code?

This is my main page which contains the loop:

@foreach (var myItem in Model.Members){

        //if i = 1
        <div class="grid_20">
        <!-- Start Row -->

        //is there someway to get in for i = 1 to 4 and pass to partial?
        @Html.Partial("nameOfPartial", Model)

        //if i = 4 then output below and reset i to 1
        <div class="clear"></div>
        <!-- End Row -->
        </div>

}

I figure I can create a int that I can update on each pass and render the text no problem here but it's passing the integer value into my partial I'm more concerned about. Unless there's a better way.

Here is my partial:

@{
switch()
case 1:
        <text>
        <div class="grid_4 alpha">
        </text>
break;
case 4:
        <text>
        <div class="grid_4 omega">
        </text>
break;
default:
        <text>
        <div class="grid_4">
        </text>
break;
}

        <img src="Content/960-grid/spacer.gif" style="width:130px; height:160px; background-color:#fff; border:10px solid #d3d3d3;" />
        <p><a href="member-card.html">@Model.Name</a><br/>
        @Model.Job<br/>
        @Model.Location</p>
</div>

Not sure if I'm having a blonde day today and this is frightfully easy but I just can't think of the best way to pass the int value in. Hope someone can help.

解决方案

 @{int i = 0;}
 @foreach(var myItem in Model.Members)
 {
     <span>@i</span>
     i++;
 }

这篇关于获取有关剃须刀的foreach索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆