Asp.net mvc mvc视图表行数计数器
我有一张表格,上面显示了我的模型的一条记录。例如,类别列表。 我想手动创建此表的行号Asp.net mvc mvc视图表行数计数器,asp.net-mvc,model-view-controller,razor,view,Asp.net Mvc,Model View Controller,Razor,View,我有一张表格,上面显示了我的模型的一条记录。例如,类别列表。 我想手动创建此表的行号 <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*
<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>
您可以使用选择重载:
<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>
定义计数器行号并写入
<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>
@{ int rowNo = 0; }
@foreach (var item in Model) {
<tr style="background: @classtr;" >
<td>
@(rowNo += 1)
</td>
定义计数器行号并写入
<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>
@{ int rowNo = 0; }
@foreach (var item in Model) {
<tr style="background: @classtr;" >
<td>
@(rowNo += 1)
</td>
以上这些对我都不起作用——我做到了:
@{int RowNo = 0;}
@foreach (var item in Model) {
<tr>
<td>@{RowNo++;} @RowNo</td>
我找到了ASP.NETMVC项目的下一个选项。以上所有选项都不适用于我的项目
@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>
}
我认为这个代码可以解决这个问题 让rows=document.querySelectorAll'tr'; rows.forEachrow=>{ 设z=document.createElementtd; z、 textContent=`row${row.rowIndex}`; 第2行,第2行; }; 项目价格 香蕉2美元 橘子8美元 上等牛腰肉20美元 总共30美元
在pged列表中,这有一个bug。此cde不知道页码,并且pagesize@item.index从零开始,因此@item.index+1更好。@MehdiDehghaniIt取决于您希望它的技术程度,尽管我同意从1开始似乎更方便用户。但这并没有改变答案的本质。