0

我从数据库中获取大量行。每行都与模型的一个对象相关联。所以现在,多亏了强类型视图,我正在传递这样的对象列表:

 public PartialViewResult ListerIncidentsHotline()
    {
        int NumDossier = StructureData.DonneNumDossier((string)Session["Utilisateur"], (string)Session["MotDePasse"]);
        List<IncidentHotline> ListeIncidents = StructureData.DonneIncidentsHotline(NumDossier, 10);
        int NbreIncidents = StructureData.DonneNombreIncidents(NumDossier);
        ViewBag.NombreIncidents = NbreIncidents;
        return this.PartialView(ListeIncidents);
    }

因此,在视图中,我正在显示一个包含以下数据的表格:

 <table id="tabIncident" class="table table-striped">
    <th>IdIncident</th><th>Libelle</th><th>Motif</th><th>Nom du responsable</th><th>Date de création</th><th>Date de clôture</th>
    @foreach (var Incident in Model)
    {  
        <tr><td>@Incident.IdIncident</td><td>@Incident.LibelleIncident</td><td>@Incident.MotifIncident</td><td>@Incident.NomResponsable</td><td>@Incident.DateCreation</td><td>@Incident.DateCloture</td></tr>
    }
</table>

但是现在我想在表格中显示其中的 10 行,然后通过单击一个按钮来显示接下来的 10 行。有人有想法吗?

4

2 回答 2

0

您的函数可能如下所示:

public PartialViewResult ListerIncidentsHotline(int page = 1)

使用局部视图进行分页有点棘手。本质上,您的父视图还需要有一个页码参数,并且它需要将该信息传递给部分视图。

然后你可能想改变你的模型。现在,您的模型本身就是您的域模型的 IEnumerable。您需要一个包含此 IEnumerable 的模型,但还具有分页信息,例如总页数和当前页。

所以你的模型看起来像:

public class IncidentPageInfo
{
    public int NumPages { get; set; }
    public int CurrentPage { get; set; }
    public int PageSize { get; set; }
    public IEnumerable<Incident> Incidents { get; set; }
}

然后,当返回 View 时,您会将此对象作为模型传递,并且您将填充页面,如下所示:

public PartialViewResult ListerIncidentsHotline(int page = 1)
{
    // other code

    const int PageSize = 10;

    IEnumerable<Incident> incidents; // this is returned from your data persistence.  IQueryable is preferred to IEnumerable

    var viewModel = new IncidentPageInfo()
    {
        NumPages = (int)Math.Ceiling((double)incidents.Count() / PageSize),
        CurrentPage = page,
        PageSize = PageSize,
        Incidents = incidents.Skip((page - 1) * PageSize).Take(PageSize),
    };

    return PartialView(viewModel);
}
于 2013-08-13T15:23:23.367 回答
0

eseaist 的方式就是使用分页库。Nuget 中有很多,但最受欢迎的似乎是PagedList。阅读这里如何实施。

于 2013-08-13T15:44:54.520 回答