Simple increment of a local variable in views in ASP.NET MVC3 (Razor)

@{
    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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)