Commit 1e11eb19 authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#3 Добавил пагинацию.

parent 310d343a
......@@ -20,4 +20,8 @@
<_ContentIncludedByDefault Remove="Views\Home\Privacy.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\uploads" />
</ItemGroup>
</Project>
......@@ -7,6 +7,7 @@ using CafeCritic.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace CafeCritic.Controllers;
......@@ -23,11 +24,20 @@ public class CafeController : Controller
}
[HttpGet]
public IActionResult Index()
public async Task<IActionResult> Index(int page = 1)
{
List<Cafe> models = _db.Cafes.ToList();
return View(models);
int pageSize = 10;
IQueryable<Cafe> source = _db.Cafes;
var count = await source.CountAsync();
var items = await source.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
PageViewModel pageViewModel = new PageViewModel(count, page, pageSize);
CafeViewModel viewModel = new CafeViewModel
{
PageViewModel = pageViewModel,
Cafes = items
};
return View(viewModel);
}
[HttpGet]
......
......@@ -4,7 +4,8 @@ namespace CafeCritic.ViewModels;
public class CafeViewModel
{
public List<Cafe> Cafes { get; set; } = new();
public IEnumerable<Cafe> Cafes { get; set; }
public PageViewModel PageViewModel { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Image { get; set; }
......
using CafeCritic.Models;
namespace CafeCritic.ViewModels;
public class IndexViewModel
{
public IEnumerable<Cafe> Cafes { get; set; }
public PageViewModel PageViewModel { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Image { get; set; }
public string? Description { get; set; }
}
\ No newline at end of file
namespace CafeCritic.ViewModels;
public class PageViewModel
{
public int PageNumber { get; set; }
public int TotalPages { get; set; }
public PageViewModel(int count, int pageNumber, int pageSize)
{
PageNumber = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
}
public bool HasPreviousPage
{
get
{
return (PageNumber > 1);
}
}
public bool HasNextPage
{
get
{
return (PageNumber < TotalPages);
}
}
}
\ No newline at end of file
@model List<Cafe>
@model CafeCritic.ViewModels.CafeViewModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
@if (Model.Count == 0)
@if (Model.Cafes.Count() == 0)
{
<h2 class="display-4">Заведения еще не добавлены.</h2>
}
else
{
<h2 class="display-4 mb-5">Список заведений</h2>
@foreach (var m in Model)
{
<div class="card d-flex mb-3 col-3" style="width: 18rem;">
<a asp-action="Details" asp-route-id="@m.Id">
<img src="@m.Image" class="card-img-top" alt="PhotoCafe">
</a>
<div class="card-body">
<a asp-action="Details" asp-route-id="@m.Id">@m.Title</a>
<table class="table">
@foreach (var m in Model.Cafes)
{
<div class="card float-start m-3 col-4" style="width: 22rem;">
<a asp-action="Details" asp-route-id="@m.Id">
<img src="@m.Image" class="card-img-top" alt="PhotoCafe">
</a>
<div class="card-body">
<a asp-action="Details" asp-route-id="@m.Id">@m.Title</a>
</div>
</div>
</div>
}
}
</table>
@if (Model.PageViewModel.HasPreviousPage)
{
<a asp-action="Index" asp-route-page="@(Model.PageViewModel.PageNumber - 1)"
class="btn btn-default btn"><i class="glyphicon glyphicon-chevron-left"></i>Назад</a>
}
@if (Model.PageViewModel.HasNextPage)
{
<a asp-action="Index" asp-route-page="@(Model.PageViewModel.PageNumber + 1)"
class="btn btn-default btn">Вперёд<i class="glyphicon glyphicon-chevron-right"></i></a>
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment