49

Well, this is just embarrassing. I can't even figure out a simple increment in one of my views in ASP.NET MVC3 (Razor). I have done searches and it appears that the documentation for Razor is pretty sparse. Here is what I have tried and failed miserably:

@{
    var counter = 1;

    foreach (var item in Model.Stuff) {
        ... some code ...
        @{counter = counter + 1;}
    }
}   

I have also tried @{counter++;} just for kicks and to no avail =) I would appreciate it if someone could enlighten me. Thanks!

6 Answers 6

76
@{
    int counter = 1;

    foreach (var item in Model.Stuff) {
        ... some code ...
        counter = counter + 1;
    }
}  

Explanation:

@{
// csharp code block
// everything in here is code, don't have to use @
int counter = 1;
}

@foreach(var item in collection){
    <div> - **EDIT** - html tag is necessary for razor to stop parsing c#
    razor automaticaly recognize this as html <br/>
    this is rendered for each element in collection <br/>
    value of property: @item.Property <br/>
    value of counter: @counter++
    </div>
}
this is outside foreach
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I have tried both. The first one at least loads the page but the foreach loop does not make it past the first loop which tells me something went wrong with the counter. The second suggestion leads to a compilation error.
Yes, using int counter worked for me. Thanks for the help! =)
It seems that dynamic compilation does not do well with type inference... You're welcome!
22

I didn't find the answer here was very useful in c# - so here's a working example for anyone coming to this page looking for the c# example:

@{
int counter = 0;
}


@foreach(Object obj in OtherObject)
{
    // do stuff
    <p>hello world</p>
    counter++;
}

Simply, as we are already in the @foreach, we don't need to put the increment inside any @{ } values.

Comments

15

If all you need to do is display the numbering, an even cleaner solution is increment-and-return in one go:

@{
   var counter = 0;
}
<section>
    @foreach(var hat in Ring){
         <p> Hat no. @(++counter) </p>
    }        
</section>

1 Comment

Ditto. I think the key is the parenthesis around ++counter.
4

See Following code for increment of a local variable in views in ASP.NET its work fine for me try it.

var i = 0; 
@foreach (var data in Model)
{
    i++; 
    <th scope="row">
          @i
    </th>....
}...

Comments

2

Another clean approach would be:

<div>
@{int counter = 1;
 foreach (var item in Model)
 {
   <label>@counter</label>
   &nbsp;
   @Html.ActionLink(item.title, "View", new { id = item.id })
   counter++;
 }
}
</div>

Comments

0

In some cases I like to 'attach' an index (counter) to the list in the foreach with select(). This example uses a list of strings but can be done with list of objects. Simply add ".Select((value, index) => new { value, index })" to the end of your list and access it with something like @item.value.property1

@code {
    List<string> strings = new() { "a", "b", "c" };
}    

@foreach (var item in strings.Select((value, index) => new { value, index }))
{
    <p>@item.value : @item.index</p>
}

Output: a : 0 b : 1 c : 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.