在项目文件夹中,创建 PaginatedList类,然后用以下代码替换模板代码。

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace FineUICore.Examples
{
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)//count:总条数 ,pageIndex:当前页,pageSize:每页显示行数
{
PageIndex = pageIndex;
TotalPages = (int)Math.Ceiling(count / (double)pageSize); this.AddRange(items);
}
//判断是否为首页
public bool HasPreviousPage
{
get
{
return (PageIndex > );
}
}
//判断是否为尾页
public bool HasNextPage
{
get
{
return (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 - ) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}
}

在 StudentsController.cs 中,将 Index方法替换为以下代码。

public async Task<IActionResult> Index(
string sortOrder,
string currentFilter,
string searchString,
int? page)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != null)
{
page = ;
}
else
{
searchString = currentFilter;
} ViewData["CurrentFilter"] = searchString; var students = from s in _context.Students
select s;
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.LastName.Contains(searchString)
|| s.FirstMidName.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "Date":
students = students.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default:
students = students.OrderBy(s => s.LastName);
break;
} int pageSize = ;
return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), page ?? , pageSize));
}

在 Views/Students/Index.cshtml 中,将现有代码替换为以下代码。

@model PaginatedList<ContosoUniversity.Models.Student>

@{
ViewData["Title"] = "Index";
} <h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p> <form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Find by name: <input type="text" name="SearchString" value="@ViewData["currentFilter"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form> <table class="table">
<thead>
<tr>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]">Last Name</a>
</th>
<th>
First Name
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]">Enrollment Date</a>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table> @{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
} <a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @nextDisabled">
Next
</a>

.net core模糊查询及分页的更多相关文章

  1. Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

    本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...

  2. django实战(二)--带多字段模糊查询的分页(也是不容易)

    上节我们实现了分页功能,这节我们要实现对模糊查询后的结果进行分页.(引入了bootstrap框架) urls.py from django.urls import path from . import ...

  3. 利用DetachedCriteria实现模糊查询和分页

      分类: Java-Developing  前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单 ...

  4. Neo4j模糊查询及分页查询

    Neo4j模糊查询:采用正则方式: MATCH (n:House) where n.Name =~ '李.*' RETURN n 分页: 使用skip 及 limit MATCH (n:House) ...

  5. Mysql里查询字段为Json格式的数据模糊查询以及分页方法

    public void datagrid(CustomFormEntity customForm,HttpServletRequest request, HttpServletResponse res ...

  6. 【Django+Element UI】使用一个接口文件,搞定分页获取数据,模糊查询后分页获取数据

    1:序列化获取数据的接口设计 1:分页获取序列化数据 2:是个能传参数的接口 class Society(APIView): def post(self, request): keywords = s ...

  7. Thinkphp5 post提交模糊查询带分页如何保留参数

    最近做了一个分页的模糊查询post请求,发现查出来的分页点击下一页导致所有的搜索条件被重置,分页效果就失效了. 以下是网上部分解决办法: 控制器代码 public function index($na ...

  8. jsp+servlet实现模糊查询和分页效果

    ---恢复内容开始--- 1.DAO+MVC包 2.DAO接口方法定义 package com.wanczy.dao; import java.math.BigDecimal;import java. ...

  9. Bootstrap-table学习笔记(二)——前后端分页模糊查询

    在使用过程中,一边看文档一边做,遇到了一些困难的地方,在此记录一下,顺便做个总结: 1,前端分页 2,后端分页 3,模糊查询 前端分页相当简单,在我添加了2w条测试数据的时候打开的很流畅,没有卡顿. ...

随机推荐

  1. NX二次开发-创建临时坐标系UF_CSYS_create_temp_csys

    NX9+VS2012 #include <uf.h> #include <uf_csys.h> #include <uf_mtx.h> UF_initialize( ...

  2. 一次修复MySQL数据库的经历

    一次修复MySQL数据库的经历 实验室服务器的硬盘满了,结果导致一个线上服务的MySQL数据库的两个表坏了.具体症状是desc cdb_searchindex显示 ERROR 1017 (HY000) ...

  3. 基于Netty的RPC架构学习笔记(三):netty客户端

    文章目录 举个

  4. 简介Python正则表达式

    一.概念 简单来说正则表达式是由一些普通字符(例如,a 到 z 之间的字母)和一些元字符组成,用来匹配和过滤一些字符串的一种逻辑公式. 二.正则表达式的一些基本规则 1.一些常用的元字符   ^  : ...

  5. jquery操作html元素之(设置内容和属性)

    设置内容 - text().html() 以及 val() 我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容( ...

  6. SpringMVC(day1搭建SpringWebMvc项目)

    MVC和webMVC的区别 Model(模型) 数据模型,提供要展示的数据,因此包含数据和行为,行为是用来处理这些数据的.不过现在一般都分离开来:Value Object(数据) 和 服务层(行为). ...

  7. spring boot 结合jsp简单示例

    引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  8. Jmeter----请求依赖之边界值提取器

    填写左边界和右边界 引用变量名就是要存储的变量名词

  9. 解决通过vmware克隆虚拟机后,无法上网的问题

    注意:如果源主机是CentOS 6.8,复制出来的机器会出现无法上网. 如果源主机是CentOS 7,复制出来的机器可以正常上网.复制后,只要改下IP地址即可上网. 出现该问题的原因是,我们克隆后,将 ...

  10. SpringMVC向前台传输 JSON数据

    所需Jar包jackson-core.jackson-annotations和jackson-databind 在MVC的配置文件中加入<mvc:annotation-driven>< ...