那么这只是尴尬。我甚至不能在ASP.NET MVC3(Razor)中看到一个简单的增量。我已经做了搜索,看来Razor的文档是相当稀疏的。这是我尝试过的并且失败的惨败:
@{ var counter = 1; foreach (var item in Model.Stuff) { ... some code ... @{counter = counter + 1;} } }
我也试过@ {counter;}只是为了踢,无济于事)如果有人能启发我,我会感激的。谢谢!
解决方法
@{ int counter = 1; foreach (var item in Model.Stuff) { ... some code ... counter = counter + 1; } }
说明:
@{ // 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