简体   繁体   中英

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>
 }

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

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

This should work:

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

You can not use like this.

         <td>
            <span>@i++;</span>
        </td>

you need to write your code as like :

         <td>
            <span>@i</span>
        </td>

You need to increase value of i after <tr> tag. I have given solution with your code.

                              @{
                                   var i = 1; @*sr no will start from 1*@
                                }
                                <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>
                                        i++;
                                    }

Hope it will work fine.

As per @Stephen Muecke suggested you can use:

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

Or you can use like this:

<span>@i</span> 
 i++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM