在 razor foreach 上获取索引值 [英] Getting index value on razor foreach

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

问题描述

我正在一个 razor foreach 循环中迭代 List 在我的视图中呈现部分.在部分中,我正在渲染一个记录,我希望在我的视图中连续显示 4 个记录.我有一个用于两端列的 css 类,因此需要在部分中确定调用是第 1 条记录还是第 4 条记录.在我的部分中识别这一点以输出正确代码的最佳方法是什么?

这是我的主页,其中包含循环:

@foreach(Model.Members 中的 var myItem){//如果我= 1<div class="grid_20"><!-- 起始行-->//有什么办法可以进入 i = 1 到 4 并传递给部分?@Html.Partial("nameOfPartial", 模型)//如果i = 4,则在下面输出并将i重置为1<div class="clear"></div><!-- 结束行-->

}

我想我可以创建一个 int ,我可以在每次传递时更新它并在此处渲染文本没有问题,但是它将整数值传递到我更关心的部分中.除非有更好的方法.

这是我的部分:

@{转变()情况1:<文本><div class="grid_4 alpha">休息;案例4:<文本><div class="grid_4 omega">休息;默认:<文本><div class="grid_4">休息;}<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>

不确定我今天是否度过了美好的一天,这非常简单,但我想不出传递 int 值的最佳方法.希望有人能提供帮助.

解决方案

 @{int i = 0;}@foreach(模型成员中的var myItem){<span>@i</span>@{i++;}}

//使用@{i++ 来增加值}

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

// Use @{i++ to increment value}

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆