Search code examples
asp.net-mvc-3entity-framework-4

Serial Number (Record Count) in Razor?


Could some one point out how to include a Serial Number while populating from a table on a Razor page view.

Here's my view

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

}

(Im using EF 4.1 scaffolding to autogenerate the contexts and models)


Solution

  • @item.Serial
    

    If you don't want to show it in an html control. You don't need to do anything special.

    EDIT: It seems you just want a counter change the loop in your code to

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

    }