Razor的序列号(记录计数)?

时间:2011-09-29 05:23:20

标签: asp.net-mvc-3 entity-framework-4

有人可以指出如何在Razor页面视图中的表格中填充序列号

这是我的观点

@foreach (var item in Model) {
    <tr>
        <td>
            ---- ??? HOW TO INCLUDE SERIAL NUMBER HERE ??? ----
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.studentName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.studentID }) |
            @Html.ActionLink("Details", "Details", new { id = item.studentID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.studentID })
        </td>
    </tr>

}

(我使用EF 4.1脚手架自动生成上下文和模型)

6 个答案:

答案 0 :(得分:7)

@item.Serial

如果您不想在html控件中显示它。你不需要做任何特别的事情。

编辑:看来你只想要一个柜台 将代码中的循环更改为

@foreach (var item in Model.Select((x, i) => new { Data = x, Index = i }))
{
<tr>
    <td>
        @item.Index
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Data.studentName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.Data.studentID }) |
        @Html.ActionLink("Details", "Details", new { id = item.Data.studentID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.Data.studentID })
    </td>
</tr>

}

答案 1 :(得分:4)

@foreach (var item in Model ) 
{
    <tr>
          <td> @( ((Int32) 1) + @Model.IndexOf(item) ) </td>
    </tr>
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

尝试一下

@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.studentName)
</td>
<td>
    @Html.ActionLink("Edit", "Edit", new { id = item.Data.studentID }) |
    @Html.ActionLink("Details", "Details", new { id = item.Data.studentID }) |
    @Html.ActionLink("Delete", "Delete", new { id = item.Data.studentID })
</td>

答案 5 :(得分:0)

@{ int i = 1; }
@foreach (var item in Model) {
    <tr>
        <td>
            @i
        </td>
        @{ i++; }
        <td>
            @Html.DisplayFor(modelItem => item.studentName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.studentID }) |
            @Html.ActionLink("Details", "Details", new { id = item.studentID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.studentID })
        </td>
   </tr>