如何在同一控制器和视图的多数据上下文中使用 PagedList

How to use PagedList in a multiple data context in the same controller and view

我有一个控制器,其中包含来自数据库的 3 个数据上下文,需要在 3 个不同的 table 的同一视图中显示它们。我需要使用 PagedList,因为我在 tables 中收到了富有表现力的卷数据。但是当改变一个页面 table 时,其他 table 也会改变。

我尝试了 pagepageNumber 的变量以及它们各自的 ViewBag 数据,但它不起作用。

视图模型:

public sealed class FileViewModel
{
    private readonly IFileDataContext _context;

    static FileViewModel()
    {

    }
    private FileViewModel()
    {
        _context = new FileDataContext();
    }

    private static readonly FileViewModel fileViewModel = new FileViewModel();
    public static FileViewModel Instance { get { return fileViewModel; } }
    public FileData FileDados { get; set; }
    public IPagedList<FileDocs> FileDocs { get; set; }
    public IEnumerable<FileDocs> ServicoDocs { get; set; }
    public IPagedList<FileRequisition> Requisicoes { get; set; }
    public IPagedList<FileService> Servicos { get; set; }
    public IEnumerable<FileService> ServicosAgrupado { get; set; }
}

控制器:

public ActionResult GetFile(int fileCodigo, int? page)
{
    //fileCodigo = 190562;

    //Variaveis
    int pageSize = 5;
    int pageNumber = (page ?? 1);
    ViewBag.FileCodigo = fileCodigo;

    var docsGeraisFile = Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories).ToList();

    //GetFile
    FileViewModel.Instance.FileDados = _context.GetFileByFileCode(fileCodigo).FirstOrDefault();

    //GetFileDocs
    FileViewModel.Instance.FileDocs = _context.GetFileIntraByFile(fileCodigo)
        .Where(fd => fd.NivelArquivo == fileCodigo.ToString()).ToPagedList(pageNumber, pageSize);

    //GetServicos
    FileViewModel.Instance.Servicos = _context.GetServicosByFile(fileCodigo).DistinctBy(s => s.CodServico).ToPagedList(pageNumber, pageSize);

    //GetServiceDocs
    var fs = _context.GetServicosByFile(fileCodigo)
        .Select(s => s.CodServico).ToList();
    FileViewModel.Instance.ServicoDocs = _context.GetFileIntraByFile(fileCodigo)
        .Where(ds => ds.NivelArquivo == fs.FirstOrDefault()).ToList();

    FileViewModel.Instance.Requisicoes = _context.GetRequisitionByFile(fileCodigo).DistinctBy(s => s.CodRequisicao).ToPagedList(pageNumber, pageSize);

    return View("Index", FileViewModel.Instance);
}

和 3 个局部视图:

@model AbreuBrasilWebFile.Web.Core.ViewModel.FileViewModel
@using PagedList.Mvc;

@if (Model.FileDocs != null)
{
    <table class="table table-bordered table-condensed table-striped">
        <thead>
            <tr>
                <th><button id="btn-show" class="btn btn-default"><i class="glyphicon glyphicon-plus"></i></button></th>
                <th>Documentos</th>
                <th></th>
                <th>CTB</th>
                <th>COM</th>
                <th>Site</th>
            </tr>
        </thead>
        <tbody>
        @foreach (var itemDocs in Model.FileDocs)
        {
            <tr>
                <td class="hidden">@itemDocs.Id</td>
                <td>@itemDocs.NomeArquivo</td>
                <td>
                    <span class="input-group-addon">
                        <button aria-hidden="false" class="btn btn-default">
                            <i class="glyphicon glyphicon-remove"></i>
                        </button>
                        <button aria-hidden="false" class="btn btn-default">
                            <i class="glyphicon glyphicon-search"></i>
                        </button>
                        <button aria-hidden="false" class="btn btn-default">
                            <i class="glyphicon glyphicon-envelope"></i>
                        </button>
                    </span>
                </td>
                <td><i class="glyphicon glyphicon-eye-open">@itemDocs.CTB</i></td>
                <td><i class="glyphicon glyphicon-eye-close">@itemDocs.COM</i></td>
                <td><i class="glyphicon glyphicon-eye-open">@itemDocs.Site</i></td>
            </tr>
        }
        </tbody>
    </table>
}
@if (Model.FileDocs.PageCount > 1)
{
    <div id="container" style="margin-left: 20px">
        <p></p>
        <p></p>
        <div class="pagination" style="margin-left: 400px">
            Page @(Model.FileDocs.PageCount < Model.FileDocs.PageNumber ? 0 : Model.FileDocs.PageNumber) of @Model.FileDocs.PageCount
            @Html.PagedListPager(Model.FileDocs, page => Url.Action("GetFile", "Files", new { page, pageSize = Model.FileDocs, fileCodigo = ViewBag.FileCodigo }))
        </div>
    </div>
}

@model AbreuBrasilWebFile.Web.Core.ViewModel.FileViewModel
@using PagedList.Mvc;

@if (Model.Requisicoes != null)
{
    <table class="table table-bordered table-condensed table-striped">
        <caption>Requisições</caption>
        <thead>
            <tr>
                <th>Num Req Files</th>
                <th>Tipo Pgto</th>
                <th>Forma Pgto</th>
                <th>Valor Recibo</th>
                <th>Validado CTB</th>
                <th>Serviços Incluídos</th>
                @*<th>Documentos Anexados</th>
                <th></th>
                <th>CTB</th>
                <th>Comercial</th>
                <th>Site</th>*@
            </tr>
        </thead>
        <tbody>
        @foreach (var itemRequisicao in Model.Requisicoes.Take(4))
        {
            <tr>
                <td>@itemRequisicao.CodRequisicao</td>
                <td>@itemRequisicao.Name</td>
                <td>@itemRequisicao.FormaPgto</td>
                <td>@itemRequisicao.ValorRecibo</td>
                <td>@itemRequisicao.ValidadoCtb</td>
            </tr>
        }
        </tbody>
    </table>
}

@model AbreuBrasilWebFile.Web.Core.ViewModel.FileViewModel
@using PagedList.Mvc;

@* File Serviços *@
<table id="#myTable" class="table>
    <caption>Serviços</caption>
    <thead>
        <tr>
            <th>Código</th>
            <th>Descrição Resumida</th>
            <th>Status</th>
            <th>Identificador Reserva</th>
            <th>Paxs</th>
            @*<th>Documentos Anexados</th>
            <th></th>
            <th>CTB</th>
            <th>Comercial</th>
            <th>Site</th>*@
        </tr>
    </thead>
    <tbody>
    @foreach (var itemSe in Model.Servicos)
    {
        <tr>
            <td>@itemSe.CodServico</td>
            <td>@itemSe.DescTipo @itemSe.Identificador </td>
            <td>@itemSe.StatusReserva</td>
            <td>@itemSe.IdentReserva</td>
            <td>@itemSe.PaxsReserva</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    }
    </tbody>
</table>
@if (Model.Servicos.PageCount > 1)
{
    <div id="container" style="margin-left: 20px">
        <p></p>
        <p></p>
        <div class="pagination" style="margin-left: 400px">
            Page @(Model.Servicos.PageCount < Model.Servicos.PageNumber ? 0 : Model.Servicos.PageNumber) of @Model.Servicos.PageCount
            @Html.PagedListPager(Model.Servicos, page => Url.Action("GetFile", "Files", new { page, pageSize = Model.Servicos, fileCodigo = ViewBag.FileCodigo }))
        </div>
    </div>
}

预计每个 table 中的分页独立移动。

所以,我有 3 个 table,并为每个 table 设置了 pageNumber 和 page 的变量。这不是最佳做法,但可以暂时解决我的问题。