row span using MVC5

杀马特。学长 韩版系。学妹 提交于 2019-12-23 02:52:23

问题


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

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


回答1:


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.



来源:https://stackoverflow.com/questions/42848883/row-span-using-mvc5

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