I am trying to build an application like in the tutorial in Microsoft: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-6.0
So i have a model like this:
using ABCrm2.Data.Account;
using ABCrm2.Models.Allgemein;
using ABCrm2.Models.Vertrieb;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
namespace ABCrm2.Models.Stammdaten
{
public class Land
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[NotNull]
[MaxLength(40)]
public string? Landbezeichnung { get; set; }
[MaxLength(3)]
public string? LandKurz { get; set; }
[Required]
[NotNull]
[MaxLength(10)]
public string? LandVorwahl { get; set; }
public DateTime? ErstellDatumZeit { get; set; }
public DateTime? ÄnderDatumZeit { get; set; } = DateTime.Now;
public bool Aktiv { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public ICollection<Bundesland>? Bundesländer { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
namespace ABCrm2.Models.Stammdaten
{
public class Bundesland
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[Required]
[NotNull]
[MaxLength(30)]
public string BundeslandText { get; set; }
[ForeignKey("Länder")]
[Required]
[NotNull]
public int LandId { get; set; }
public Land? Länder { get; set; }
public bool Aktiv { get; set; }
public DateTime? ErstellDatumZeit { get; set; }
public DateTime? ÄnderDatumZeit { get; set; } = DateTime.Now;
[Timestamp]
public byte[] RowVersion { get; set; }
}
}
in the Index of Land i use the helper PaginatedList.cs like in the tutorial:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace ContosoUniversity
{
public class PaginatedList<T> : List<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
this.AddRange(items);
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
}
in the details of land i would like to have the land and the list of countries (Bundesland)
Controller Part:
public async Task<IActionResult> Details(int? id, int? seiteBL)
{
if (id == null || _context.Länder == null)
{
return NotFound();
}
var land = await _context.Länder
.AsNoTracking()
.Include(a => a.Bundesländer.Where(s => s.Aktiv.Equals(true)))
.FirstOrDefaultAsync(m => m.Id == id);
if (land == null)
{
return NotFound();
}
var pageSizeBL = Configuration.GetValue("PageSize", 16);
double AnzahlTrefferBL = land.Bundesländer.Count();
int? aktuelleSeiteBL = seiteBL;
if (aktuelleSeiteBL == null)
{
aktuelleSeiteBL = 1;
}
ViewData["AnzahlTrefferBL"] = land.Bundesländer.Count();
ViewData["AnzahlSeitenBL"] = (int)Math.Ceiling(AnzahlTrefferBL / pageSizeBL);
ViewData["AktuelleSeiteBL"] = aktuelleSeiteBL;
//how do i get the paging of Bundesländer?
return View(land);
}
the view:
@using ABCrm2.Helper
@using ABCrm2.Models.Stammdaten
@model Land
@{
ViewData["Title"] = "Land anzeigen";
}
<form method="post" asp-action="Details">
<div class="row">
<div class="col-1">
<div class="input-group-sm mb-1">
<h2><input disabled type="text" asp-for="Id" class="form-control trechts" title="die eindeutige Nummer des Landes" /></h2>
</div>
</div>
<div class="col-11">
<h2>Land anzeigen</h2>
</div>
<hr />
</div>
<div class="row">
<div class="col-1">
<a asp-area="" asp-controller="Land" asp-action="Edit" asp-route-id= "@Model.Id" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="top" title="Land ändern">
<i class="bi bi-pencil-square"></i>
</a>
<a asp-area="" asp-controller="Land" asp-action="Index" class="btn btn-secondary btn-sm" data-toggle="tooltip" data-placement="top" title="Zurück zur Liste">
<i class="bi bi-list-columns"></i>
</a>
<a asp-area="" asp-controller="Land" asp-action="Details" class="btn btn-refresh btn-sm" data-toggle="tooltip" data-placement="top" title="Aktualisieren">
<i class="bi bi-arrow-clockwise"></i>
</a>
</div>
<div class="col-5">
<div class="row">
<div class="border p-2">
<div class="row">
<div class="col-12">
<div class="input-group-sm mb-1">
<input asp-for="Aktiv" title="Nur aktive Länder können verwendet werden" disabled/>
<label asp-for="Aktiv" class="control-label" ></label>
</div>
</div>
</div>
<div class="row">
<div class="col-2">
<div class="input-group-sm mb-1">
<label asp-for="LandKurz"></label>
<input asp-for="LandKurz" class="form-control" title="Das Länderkürzel" disabled/>
</div>
</div>
<div class="col-10">
<div class="input-group-sm mb-1">
</div>
</div>
</div>
<div class="input-group-sm mb-1">
<label asp-for="Landbezeichnung"></label>
<input asp-for="Landbezeichnung" class="form-control" title="Das Land" disabled />
</div>
<div class="row">
<div class="col-3">
<div class="input-group-sm mb-1">
<label asp-for="LandVorwahl"></label>
<input asp-for="LandVorwahl" class="form-control" title="Die Länder-Telefonvorwahl" disabled />
</div>
</div>
<div class="col-9">
<div class="input-group-sm mb-1">
</div>
</div>
</div>
</div>
</div> @*row*@
<div class="row">
<div class="border p-2">
<div class="row">
<div class="col-4">
@Html.DisplayNameFor(model => model.ErstellDatumZeit)
</div>
<div class="col-4">
@Html.DisplayNameFor(model => model.ÄnderDatumZeit)
</div>
</div>
<div class="row">
<div class="col-4">
@Html.DisplayFor(model => model.ErstellDatumZeit)
</div>
<div class="col-4">
@Html.DisplayFor(model => model.ÄnderDatumZeit)
</div>
</div>
</div>
</div> @*row*@
</div>
<div class="col-lg-4 col-sm-11 col-md-11">
<div class="table-responsive">
<table class="table table-striped table-dark table-sm caption-top">
<caption>
<div class="row">
<div class="col-9">
<h3>Bundesländer</h3>
</div>
<div class="col-3">
</div>
</div>
</caption>
<thead>
<tr>
<th>
<div class="btn-group btn-group-sm" role="group">
<a asp-area="" asp-controller="Bundesländer" asp-action="Create" class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="Neues Bundesland anlegen">
<i class="bi bi-plus-square"></i>
</a>
<a class="btn btn-info mx-2" data-bs-toggle="offcanvas" data-placement="top" data-toggle="tooltip" data-bs-target="#suche" aria-controls="suche" title="Bundesland suchen">
<i class="bi bi-search"></i>
</a>
<a asp-area="" asp-controller="Bundesländer" asp-action="Index" class="btn btn-refresh btn-sm" data-toggle="tooltip" data-placement="top" title="Aktualisieren">
<i class="bi bi-arrow-clockwise"></i>
</a>
</div>
</th> <!-- wird benötigt für Edit, Delete Buttons -->
<th>
<a asp-action="Index" asp-route-sortierung="@ViewData["AktivSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" title="nur aktive Bundesländer können gewählt werden">Aktiv</a>
</th>
<th>
<a asp-action="Index" asp-route-sortierung="@ViewData["IdSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" title="Die eindeutige ID des Bundeslandes">Id</a>
</th>
<th>
<a asp-action="Index" asp-route-sortierung="@ViewData["BundeslandTextSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" title="Das Bundesland">Bundesland</a>
</th>
<th>
<a asp-action="Index" asp-route-sortierung="@ViewData["DateÄnderungSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" title="Datum der letzten Änderung">Änderungsdatum</a>
</th>
<th>
<a asp-action="Index" asp-route-sortierung="@ViewData["DateErstellSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" title="Datum der Ersterfassung">Erstelldatum</a>
</th>
</tr>
</thead>
<tbody>
@if (Model.Bundesländer.Count() > 0)
{
@foreach (var item in Model.Bundesländer)
{
<tr>
<td>
<div class="btn-group btn-group-sm" role="group">
<a asp-controller="Bundesländer" asp-action="Details" asp-route-id="@item.Id" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Bundesland anzeigen">
<i class="bi bi-eye"></i>
</a>
<a asp-controller="Bundesländer" asp-action="Edit" asp-route-id="@item.Id" class="btn btn-primary mx-2" data-toggle="tooltip" data-placement="top" title="Bundesland ändern">
<i class="bi bi-pencil-square"></i>
</a>
<a asp-controller="Bundesländer" asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger" data-toggle="tooltip" data-placement="top" title="Bundesland löschen">
<i class="bi bi-trash-fill"></i>
</a>
</div>
</td>
<td>@Html.DisplayFor(modelItem => item.Aktiv)</td>
<td>@Html.DisplayFor(modelItem => item.Id)</td>
<td>@Html.DisplayFor(modelItem => item.BundeslandText)</td>
<td>@Html.DisplayFor(modelItem => item.ÄnderDatumZeit)</td>
<td>@Html.DisplayFor(modelItem => item.ErstellDatumZeit)</td>
</tr>
}
}
else
{
<tr>
<td colspan="12" class="text-warning">keine Datensätze vorhanden</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="col-1">
</div>
</div>
</form>
it is all ok - the Bundesländer are listet complete - but how to do paging there for the navigation property by using the helper PaginatedList.cs ??