'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 1:[1]
@foreach (var item in Model )
{
<tr>
<td> @( ((Int32) 1) + @Model.IndexOf(item) ) </td>
</tr>
}
Solution 2:[2]
@{int index = 1;}
@foreach (var item in Model)
{
<tr>
<td>
@index
</td>
</tr>
index++;
}
Solution 3:[3]
<tbody>
@{
int sno = 0;
}
@foreach (var item in Model)
{
<tr>
<td>
@{ sno++; }
@sno
</td>
</tr>
}
</tbody>
Solution 4:[4]
@{ 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>
Solution 5:[5]
@foreach (var item in Model) {
<tr>
<td>
@(Model.ToList().IndexOf(item) + 1)
</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>
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | slfan |
| Solution 2 | R M Shahidul Islam Shahed |
| Solution 3 | amarnath |
| Solution 4 | Rahat Hossain |
| Solution 5 | Jefril Jef |
