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!
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:
Simply, as we are already in the @foreach, we don't need to put the increment inside any @{ } values.
See Following code for increment of a local variable in views in ASP.NET its work fine for me try it.
If all you need to do is display the numbering, an even cleaner solution is increment-and-return in one go:
Another clean approach would be:
Explanation: