2013-08-13 4 views
0

데이터베이스에서 많은 수의 행이 나타납니다. 각 행은 모델의 객체에 연결됩니다. 그래서 지금, 강력한 형식의보기 덕분에 나는 그와 같은 객체의 목록을 통과하고있어 whiere :10 개 행의 테이블에서 모델의 데이터를 페이지 매기기하는 방법은 무엇입니까?

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

을 그래서보기에 내가 그 같은 datas있는 테이블을 표시 해요 :

<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 개의 행을 표시합니다. 누군가 아이디어가 있습니까?

답변

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

:

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); 
} 
+0

감사합니다. 정말 재미 있습니다. 다른 사람들의 문제를 해결하는 방법도 나와 있습니다. 대단 하네! –

0

eseaist처럼

그래서 당신의 모델이 보일 것이다 방법은 페이지 라이브러리를 사용하는 것입니다. Nuget에는 많은 것들이 있지만 가장 인기있는 것은 PagedList 인 것 같습니다. 구현 방법은 here을 읽으십시오.

관련 문제