使用MVC5的行跨度 [英] row span using MVC5

查看:73
本文介绍了使用MVC5的行跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何使用MVC4在 PerferenceNo JobName 上使用行跨度

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

推荐答案

您需要将以前的值与当前值进行比较,但是不需要使用rowspan(这会不必要地使其复杂化).当单元格与之前的值匹配时,您只需生成一个空的<td>元素即可.

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

我强烈建议您将模型传递给视图,而不要从ViewData

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

如果您确实想使用rowspan属性,则您的代码将必须是

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.

这篇关于使用MVC5的行跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆