Asp.net mvc 4 如何在十行表中对模型中的数据进行分页?

Asp.net mvc 4 如何在十行表中对模型中的数据进行分页?,asp.net-mvc-4,pagination,Asp.net Mvc 4,Pagination,我正在从数据库中获取大量行。每行都与模型的一个对象相关联。现在,多亏了一个强类型视图,我正在传递一个对象列表,如下所示: public PartialViewResult ListerIncidentsHotline() { int NumDossier = StructureData.DonneNumDossier((string)Session["Utilisateur"], (string)Session["MotDePasse"]); List&

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

 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>
public PartialViewResult ListerIncidentsHotline()
{
int NumDossier=StructureData.donneumdossier((字符串)会话[“usilisateur”],(字符串)会话[“MotDePasse”]);
List ListEvents=StructureData.DonneIncidentsHotline(NumDossier,10);
int NbreIncidents=StructureData.DonneNombreIncidents(NumDossier);
ViewBag.NombreIncidents=NbreIncidents;
返回此。PartialView(列出事件);
}
因此,在视图中,我显示了一个包含如下数据的表:

 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>

意外诽谤罪的法定责任日
@foreach(模型中的var事件)
{  
@事件。IdIncident@Incident.LibelleIncident@事件。MotifIncident@Incident.NomResponsable@事件。DateCreation@Incident.DateCloture
}

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

您的函数可能如下所示:

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; }
}
public class IncidentPageInfo
{
公共整数{get;set;}
public int CurrentPage{get;set;}
公共int PageSize{get;set;}
公共IEnumerable事件{get;set;}
}
然后,当返回视图时,您将此对象作为模型传递,并按如下方式填充页面:

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);
}
public PartialViewResult ListerIncidentsHotline(int page=1)
{
//其他代码
常量int PageSize=10;
IEnumerable事件;//这是从数据持久性返回的。IQueryable优先于IEnumerable
var viewModel=newincidentpageinfo()
{
NumPages=(int)Math.天花((double)incents.Count()/PageSize),
CurrentPage=第页,
PageSize=PageSize,
事件=事件。跳过((第1页)*页面大小)。获取(页面大小),
};
返回部分视图(视图模型);
}

ESAIST的方法就是使用分页库。在努吉有很多,但最受欢迎的似乎是。阅读如何实施。

谢谢,它真的很有趣,它还向我展示了如何解决其他一些问题。太棒了!