Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting index value on razor foreach

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.

like image 384
lloydphillips Avatar asked Apr 26 '12 02:04

lloydphillips


People also ask

How do I get an index in foreach?

Luckily, there are several ways to get an index variable with foreach : Declare an integer variable before the loop, and then increase that one inside the loop with each loop cycle. Create a tuple that returns both the element's value and its index. Or swap the foreach loop with the for loop.

What is @model in razor?

The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

Which object can be iterated in foreach loop in C#?

Generally, in c# Foreach loop will work with the collection objects such as an array, list, etc., to execute the block of statements for each element in the array or collection.

What is Razor syntax MVC 5?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.


2 Answers

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

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

like image 124
noamtcohen Avatar answered Oct 22 '22 10:10

noamtcohen


//this gets you both the item (myItem.value) and its index (myItem.i) @foreach (var myItem in Model.Members.Select((value,i) => new {i, value})) {     <li>The index is @myItem.i and a value is @myItem.value.Name</li> } 

More info on my blog post http://jimfrenette.com/2012/11/razor-foreach-loop-with-index/

like image 25
Jim Frenette Avatar answered Oct 22 '22 10:10

Jim Frenette