asp.net-mvc mvc 视图表行号计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20141808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:20:46  来源:igfitidea点击:

mvc view table row number counter

asp.net-mvcmodel-view-controllerrazorview

提问by user8054841

I have a table than shown one record of my model. for example list of category. i want to create manually row number for this table.

我有一张桌子,上面显示了我的模型的一条记录。例如类别列表。我想为此表手动创建行号。

<table class="table table-striped table-bordered table-hover table-full-width dataTable">
  @foreach (var item in Model)
  {   
    <tr>
      <td>
        **I want to Show Row Number Here**
      </td>
      <td>
        @Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
      </td>
    </tr>
  }
</table>

回答by hasan

Define a counter rowNoand write

定义一个计数器rowNo并写入

@{ int rowNo = 0; }

@foreach (var item in Model) {       
<tr style="background: @classtr;" >
    <td>
        @(rowNo += 1)
    </td>

回答by Murali Murugesan

Define a counter rowNoand write

定义一个计数器rowNo并写入

<table class="table table-striped table-bordered table-hover table-full-width dataTable">

  @{int rowNo;}

  @foreach (var item in Model)
  {   
    <tr>
      <td>
       @rowNo++;
      </td>
      <td>
        @Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
      </td>
    </tr>
  }
</table>

回答by niico

None of the above worked for me - I did this:

以上都不适合我 - 我这样做了:

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

回答by Giannis Paraskevopoulos

You may use the Select overload:

您可以使用 Select 重载:

<table class="table table-striped table-bordered table-hover table-full-width dataTable">
  @foreach (var item in Model.Select((item,index)=>new{item,index})
  {   
    <tr>
      <td>
        @item.index
      </td>
      <td>
        @Html.ActionLink(item.item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.item.Id }, null)
      </td>
    </tr>
  }
</table>

回答by Kiril Dobrev

I've found the next option for my ASP.Net MVC project. All above options didn't work for my project.

我为我的 ASP.Net MVC 项目找到了下一个选项。以上所有选项都不适用于我的项目。

    @foreach (var item in Model.Select((x, i) => new { Data = x, Index = i + 1 }))
    {
        <tr>
            <td>
                @item.Index
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Data.ReviewerName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Data.ReviewerComments)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Data.ReviewerRating)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Data.Id })
            </td>
        </tr>
    }