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

时间:2019-04-23 12:43:53

标签: c# asp.net-mvc html-table pagedlist

我有一个具有3个数据库数据上下文的控制器,需要在3个不同表的同一视图中显示它们。我需要使用PagedList,因为我在此表中接收到富有表现力的卷数据。但是当更改页表时,其他表也更改。

我尝试为每个变量pagepageNumber以及ViewBag Data设置变量,但是它不起作用。

ViewModel:

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

希望每个表中的分页都独立移动。

1 个答案:

答案 0 :(得分:0)

因此,我有3个表,并为每个表的pageNumber和page设置变量。这不是最佳实践,但可以立即解决我的问题。