增量在asp.net MVC剃刀视图

本文关键字:MVC 剃刀 视图 net asp | 更新日期: 2023-09-27 18:09:55

我想在Sr. No.中显示以下代码的第一列的增量值。在ASP.net MVC Razor视图中。我在做下面的事情,但它不起作用。它显示0++;只在那一栏。我做错了什么。

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

增量在asp.net MVC剃刀视图

在razor语法中,你应该为c#逻辑语句创建c#作用域。你可以试试下面的代码;

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

你不能这样使用

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

你需要这样写代码:

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

需要在<tr>标签后增加i的值。我已经用你的代码给出了解决方案。

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

应该可以:

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

根据@Stephen Muecke的建议,您可以使用:

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

或者你可以这样写:

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