yiad yiad - 5 months ago 43
Javascript Question

count number of row in table asp.net mvc

I am trying to count number of row of table by created from database. For example in table from database has 3 names A, B,C. It counts A:1 B:1 C:1 in table but it should be 3.

<table class="table">
<tr>
<th>Student Name</th>

</tr>
@foreach (var item in Model.Records)
{
var count = 0;
<tr>
<td>

@Html.DisplayFor(modelItem => item.Student.Name)
@Html.DisplayFor(modelItem => item.Student.SurName)

@if (item.Room.Capacity == 4)
{

count++;
@Html.DisplayFor(modelItem => count)
}


</td>

</tr>
}
</table>

Answer

Try it like this

  <table class="table">
    <tr>
        <th>Student Name</th>

    </tr>
    @{
        var count = 0;
        foreach (var item in Model.Records)
    {

        <tr>
            <td>

                @Html.DisplayFor(modelItem => item.Student.Name)
                @Html.DisplayFor(modelItem => item.Student.SurName)

                @if (item.Room.Capacity == 4)
                {
                    count++;
                    @Html.DisplayFor(modelItem => count)
                }
            </td>

        </tr>
    }
}
</table>
Comments