使用pagedlist时获取总数

时间:2015-11-15 21:17:42

标签: c# asp.net-mvc asp.net-mvc-5 pagedlist

我在下面定义了以下代码,我要做的是获取以下属性的总数(ItemsRequireingAttention,NumberOfUrgentItems)。

虽然Model.Count告诉我在视图模型中有多少视图模型,但我希望能够得到上述2个属性的总数。

我尝试过使用Model.TotalCountFor和Model.Where,但似乎无法让他们工作。我下面的当前代码只会告诉我当前查看的子集的总数。这是可能的还是我将不得不考虑使用部分

PortalViewModel

public class PortalViewModel
{
    public Vehicle Vehicle { get; set; }
    public bool MotExpired { get; set; }
    public bool TaxExpired { get; set; }
    public bool MotWithin7Days { get; set; }
    public bool TaxWithin7Days { get; set; }
    public bool MotWithin30Days { get; set; }
    public bool TaxWithin30Days { get; set; }
    public bool MotNotRecorded { get; set; }
    public bool TaxNotRecorded { get; set; }
    public int NumberOfUrgentItems { get; set; }
    public int ItemsRequireingAttention { get; set; }

}

PortalController

public class PortalController : Controller
{
    public ActionResult Index(int? page)
    {
        var cookie = new HttpCookie("view", "admin"); 
        var portalViewModel = new List<PortalViewModel>();

        var vehicles = db.Vehicles.ToList();

        foreach (var vehicle in vehicles)
        {
            var vm = new PortalViewModel { Vehicle = vehicle };

            if (vehicle.MotDate == null)
            {
                vm.MotNotRecorded = true;
                vm.NumberOfUrgentItems++;
            }
            if (vehicle.TaxDate == null)
            {
                vm.TaxNotRecorded = true;
                vm.NumberOfUrgentItems++;
            }

            if (vehicle.MotDate <= DateTime.Now.AddYears(-1))
            {
                vm.MotExpired = true;
                vm.NumberOfUrgentItems++;
            }
            if (vehicle.TaxDate != null && DateTime.Now.Subtract((DateTime)vehicle.TaxDate).Days >= 7)
            {
                vm.TaxWithin7Days = true;
            }
            else if (vehicle.TaxDate != null && DateTime.Now.Subtract((DateTime)vehicle.TaxDate).Days >= 30)
            {
                vm.TaxWithin30Days = true;
            } 
            portalViewModel.Add(vm);
        }
    }
}

查看

@using Microsoft.Ajax.Utilities
@using PagedList
@using PagedList.Mvc;

@using WebApplication1.ViewModels
@model IPagedList<PortalViewModel>

@{
    ViewBag.Title = "Dashboard";
    var pagedList = (IPagedList)Model;
    var urgentItems = Model.Count(i => i.NumberOfUrgentItems >= 1); //(item => item.NumberOfUrgentItems >= 1);
    int flaggedItems = Model.Count(i => i.ItemsRequireingAttention >= 1);
    int vehicles = Model.Count();

}

<h2>Dashboard</h2>
<hr />
<div class="row">
    @if (urgentItems != 0)
    {
        <div class="col-md-3 col-sm-6 col-xs-6">
            <div class="panel panel-back noti-box">
                <span class="icon-box bg-color-red set-icon">
                    <i class="fa fa-warning"></i>
                </span>
                <div class="text-box">
                    <p class="main-text">@urgentItems Items</p>
                    <p class="text-muted">Requiring immediate action</p>
                </div>
            </div>
        </div>
    }
    @if (flaggedItems != 0)
    {
        <div class="col-md-3 col-sm-6 col-xs-6">
            <div class="panel panel-back noti-box">
                <span class="icon-box bg-color-green set-icon">
                    <i class="fa fa-exclamation"></i>
                </span>
                <div class="text-box">
                    <p class="main-text">@flaggedItems Items</p>
                    <p class="text-muted"><br />Require attention</p>
                </div>

            </div>
        </div>
    }
    <div class="col-md-3 col-sm-6 col-xs-6">
        <div class="panel panel-back noti-box">
            <span class="icon-box bg-color-blue set-icon">
                <i class="fa fa-truck"></i>
            </span>
            <div class="text-box">
                <p class="main-text">@vehicles</p>
                <p class="text-muted"><br />Vehicles in fleet</p>
            </div>
        </div>
    </div>
</div>
<hr />
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">

        <div class="panel panel-default">
            <div class="panel-heading text-center">
                <strong>Current Fleet Details</strong>
            </div>

            <div class="panel-body">
                <div class="table-responsive">
                    <div class="panel  panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center">
                                <span class=" ">
                                    Click on the reference number to edit vehicle details
                                </span>
                                <span class="pull-left"><i class="fa fa-info-circle"></i></span>
                                <span class="pull-right"><i class="fa fa-info-circle"></i></span>
                            </h3>
                        </div>
                    </div>
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>@Html.DisplayNameFor(m => m.First().Vehicle.RefNumber)</th>
                                <th>@Html.DisplayNameFor(m => m.First().Vehicle.Make)</th>
                                <th>@Html.DisplayNameFor(m => m.First().Vehicle.Model)</th>
                                <th>@Html.DisplayNameFor(m => m.First().Vehicle.Registration)</th>
                                <th>@Html.DisplayName("Dates")</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr class='@if (item.MotNotRecorded || item.MotExpired || item.TaxNotRecorded || item.TaxExpired)
                                               {
                                                   @Html.Raw("danger")

                                               } else if (item.MotWithin30Days || item.MotWithin7Days || item.TaxWithin30Days || item.TaxWithin7Days)
                                               {
                                                   @Html.Raw("warning")
                                               }
                                               else
                                               {
                                                   @Html.Raw("success")
                                               }'>
                                    <td>
                                        @if (!item.Vehicle.RefNumber.IsNullOrWhiteSpace() || !string.IsNullOrEmpty(item.Vehicle.RefNumber))
                                        {
                                            @Html.ActionLink(item.Vehicle.RefNumber, "Edit", "Portal", new { @ref = item.Vehicle.RefNumber }, null)
                                        }
                                        else
                                        {
                                            @item.Vehicle.RefNumber
                                        }
                                    </td>
                                    <td>
                                        @item.Vehicle.Make
                                    </td>
                                    <td>
                                        @item.Vehicle.Model
                                    </td>
                                    <td>
                                        @item.Vehicle.Registration
                                    </td>
                                    <td>
                                        @Html.LabelFor(modelItem => item.Vehicle.MotDate):
                                        @{
                                if (item.MotNotRecorded)
                                {
                                    <span class="pull-right text-warning text-uppercase">
                                        <i class="glyphicon glyphicon-warning-sign"></i>
                                        @string.Format("Not yet recorded")
                                    </span>
                                }
                                else
                                {
                                    <span class="pull-right text-success">
                                        @string.Format("{0:dd/MM/yyyy}", item.Vehicle.MotDate) <i class="glyphicon glyphicon-ok"></i>
                                    </span>
                                }
                                        }
                                        <br />
                                        @Html.LabelFor(modelItem => item.Vehicle.TaxDate)
                                        @{
                                if (item.TaxNotRecorded)
                                {
                                    <span class="pull-right text-warning text-uppercase">
                                        <i class="glyphicon glyphicon-warning-sign"></i>
                                        @string.Format("Not yet recorded")
                                    </span>
                                }
                                else
                                {
                                    <span class="pull-right text-success">
                                        @string.Format("{0:dd/MM/yyyy}", item.Vehicle.TaxDate) <i class="glyphicon glyphicon-ok"></i>
                                    </span>
                                }
                                        }
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
                    @Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您的控制器方法缺少{ "extends": "eslint:recommended" } 语句,但我假设您要返回return列表。

我建议创建一个portalViewModel IPagedList的ViewModel,并将所需的值放在包含的ViewModel中。然后,您可以在视图中为每个项目使用显示模板。

但是,您还可以创建两个PortalViewModel变量并在车辆列表中的int循环中递增它们,然后将它们放入ViewBag。

foreach