row span using MVC5

2019-06-01 10:16发布

                    @if (ViewData["post"] != null)
                    {

                        foreach (var item in ViewData["post"] as IEnumerable<Employee.Models.ApplyJob>)
                        {
                            <tr>
                                <td>@item.PerferenceNo</td>
                                <td>@item.JobName</td>
                                <td>@item.Location</td>
                                <td>Action</td>
                            </tr>
                        }
                    }

enter image description here

how to use row span on PerferenceNo and JobName using MVC4>

1条回答
萌系小妹纸
2楼-- · 2019-06-01 11:05

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.

查看更多
登录 后发表回答