【问题标题】:Razor View Html Table layoutRazor View Html 表格布局
【发布时间】:2020-03-22 13:12:40
【问题描述】:

我在 razor 视图中有一个表格,它显示这样的数据

有些字段我想这样分组

我认为我不会通过分组来实现这一点,因为我会丢失数据。不确定采取什么方法?

这是我的标记

<div class="row">
    <table class="arrowes-table table-striped">
        <thead>
            <tr>
                <th>Driver Name</th>
                <th>Alarm Type : Count</th>
                <th>Total Distance For Period</th>
                <th>Avg Per Km Driven</th>
                <th>Relative Driver Score</th>
            </tr>
        </thead>

        <tbody>
            @foreach (var item in Model.DriverBehaviour.OrderByDescending(x => x.DriverName))
            {               
                <tr>
                    <td>@Html.HiddenFor(m => item.DriverId) @Html.ActionLink(item.DriverName, "Operator", "Operators", new { area = "VehicleManagement", operatorId = item.DriverId })</td>
                    <td>@item.AlarmType : <span class="brand-red">@item.TypeCount</span></td>
                    <td>@item.TotalDistanceForPeriod</td>
                    <td>
                        @{
                            var avg = @Math.Truncate(1000 * item.AverageAlarmPerKmDriven) / 1000;
                        }
                        @avg
                    </td>
                    <td>
                        @{
                            var relScore = @Math.Truncate(1000 * @item.RelativeDriverScore) / 1000;

                            if (relScore.ToString().StartsWith("-") == true)
                            {
                                <span class="brand-red">@relScore</span>
                            }
                            if (relScore.ToString().StartsWith("-") == false)
                            {
                                <span class="brand-green">+@relScore</span>
                            }

                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

感谢您的任何想法:)

【问题讨论】:

    标签: c# html asp.net-mvc razor .net-core


    【解决方案1】:

    没关系,我知道了

    <div class="row">
        <table class="arrowes-table table-striped">
            <thead>
                <tr>
                    <th>Driver Name</th>
                    <th>Alarm Type : Count</th>
                    <th>Total Distance For Period</th>
                    <th>Avg Per Km Driven</th>
                    <th>Relative Driver Score</th>
                </tr>
            </thead>
    
            <tbody>
                @foreach (var group in Model.DriverBehaviour.GroupBy(item => item.DriverName))
                {
                    <tr>
                        <td>@Html.ActionLink(@group.Key, "Operator", "Operators", new { area = "VehicleManagement", operatorId = group.FirstOrDefault().DriverId })</td>
                        <td>
                            <ul class="list-unstyled">
                                @foreach (var item in group)
                                {
                                    <li> @item.AlarmType : <span class="brand-red">@item.TypeCount</span></li>
                                }
                            </ul>
    
                        </td>
                        <td>
                            <ul class="list-unstyled">
                                @foreach (var item in group.GroupBy(x => x.TotalDistanceForPeriod))
                                {
                                    <li>@item.Key</li>
                                }
                            </ul>
                        </td>
                        <td>
                            <ul class="list-unstyled">
                                @foreach (var item in group)
                                {
                                    var avg = @Math.Truncate(1000 * item.AverageAlarmPerKmDriven) / 1000;
    
                                    <li>
                                        @avg
                                    </li>
                                }
                            </ul>
                        </td>
                        <td>
                            <ul class="list-unstyled">
                                @foreach (var item in group)
                                {
                                    var relScore = @Math.Truncate(1000 * @item.RelativeDriverScore) / 1000;
    
                                    if (relScore.ToString().StartsWith("-") == true)
                                    {
                                        <li class="brand-red">@relScore</li>
                                    }
                                    if (relScore.ToString().StartsWith("-") == false)
                                    {
                                        <li class="brand-green">+@relScore</li>
                                    }
                                }
                            </ul>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

    产生这个

    【讨论】:

      猜你喜欢
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多