count asp.net mvc表中的行数

时间:2016-06-10 07:07:49

标签: javascript html asp.net-mvc

我正在尝试计算从数据库创建的表行数。例如,在数据库表中有3个名称A,B,C。它在表中计算A:1 B:1 C:1但它应该是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)


                </td>

            </tr>
        }
    </table>

1 个答案:

答案 0 :(得分:0)

尝试这样

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