row span using MVC5

这一生的挚爱 提交于 2019-12-08 23:03:33

You need to compare the previous values with the current values, but there is no need to use rowspan (which would unnecessarily complicate it). You can just generate an empty <td> element when the cell matches the previous value.

@{
    var data = ViewData["post"] as IEnumerable<Employee.Models.ApplyJob>;
    int perferenceNo = 0;
    string jobName = "";
}
@foreach (var item in data)
    <tr>
        @if (item.PerferenceNo == perferenceNo)
        {
            <td></td>
        }
        else
        {
            perferenceNo = item.PerferenceNo;
            <td>@item.PerferenceNo</td>
        }
        ... // ditto for JobName 
        <td>@item.Location</td>
    </tr>
}

And I strongly recommend you pass a model to the view, rather than casting it from ViewData

If you do want to use the rowspan attribute, then you code will need to be

@foreach (var item in items)
{
    int perferenceNoCount = data.Count(x => x.PerferenceNo == item.PerferenceNo);
    int jobNameCount = data.Count(x => x.JobName == item.JobName);
    <tr>
        @if (item.PerferenceNo != perferenceNo)
        {
            perferenceNo = item.PerferenceNo;
            <td rowspan="@(perferenceNoCount)">@item.ID</td>
        }
        @if (item.JobName != jobName)
        {
            jobName = item.JobName;
            <td rowspan="@(jobNameCount)">@item.Name</td>
        }
        <td>@item.Location</td>
    </tr>
}

But you really should be using view model in that case with properties for the rowspan values and run your queries in the controller.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!