2013-11-22 62 views
1

我有一張表比我的模型顯示一個記錄。例如類別列表。 我想爲此表手動創建行號。MVC查看錶行數計數器

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

回答

1

您可以使用選擇超載:

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

定義計數器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> 
8

定義計數器rowNo

@{ int rowNo = 0; } 

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

在一個pged列表,這有一個錯誤。這個cde沒有意識到pagenumber和pagesize –

0

無的AB奧夫爲我工作 - 我這樣做:

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