Search code examples
c#asp.netasp.net-mvcrazor

increment in asp.net mvc razor view


I want to display incremental values at the first column of following code inside Sr. No. column in ASP.net MVC Razor view. I am doing following but its not working. It showing 0++; only in that column. what wrong I am doing.

 @{int i = 0;}

 <table class="table">
 <tr>
    <th>Sr No.</th>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ModifiedDate)
    </th>
    <th></th>
 </tr>

 @foreach (var item in Model)
 {
    <tr>
        <td>
            <span>@i++;</span>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ModifiedDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.CurrencyCode }) |
            @Html.ActionLink("Details", "Details", new { id = item.CurrencyCode }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.CurrencyCode })
        </td>
    </tr>
 }

Solution

  • In razor syntax you should create C# scope for C# logical statemens. Can you try the the code below;

    <td>
        <span>@(i++)</span>
    </td>