Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4

row counter in mvc index view can't generate number


I am trying to put a row counter in index view of my mvc project to count the items .my code is like this :

 @{ int a = 0; }
@foreach (var item in Model) {
    <tr>
        <td>
            @a=a+1;
        </td>
     </tr>
}

But the result is 0=a+1;

Could you please give me some help .


Solution

  • This should work if you want to start counting rows from zero:

    <td>@(a++)</td>
    

    In case of counting from one, use preincrementation:

    <td>@(++a)</td>