在项目文件夹中,创建 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. CentOS下安装Lua

    Lua是一种轻量小巧的脚本语言,用标准  C语言编写并以源代码形式开放,其设计目的是 为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能.官网: http://www.lua.org/ 安装过 ...

  2. NOIp2018集训test-9-21(am/pm)

    Am DAY1 抄代码 送分题 //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++ ...

  3. centos7构建kylo-0.10.1

      构建服务器使用centos7,8G内存.建议使用8G内存,因为内存不够失败了好几次. 系统需要提前安装一下组件: yum install -y gcc bzip2 rpm-build rpmdev ...

  4. NX二次开发-UFUN批量操作图层状态UF_LAYER_set_many_layers_status

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...

  5. python之tkinter学习目录

    前言 下面的目录结构,采用的学习视频资料是网易云课堂中[莫凡]老师的,在目录的最下面的地方给出了对应的链接! 学习是逐渐积累起来的,代码也是!下面的每一篇中的对应代码,都秉承着这样的一个理念:代码是成 ...

  6. 史上最全Html和CSS布局技巧

      单列布局水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元 ...

  7. LOL遇到登录服务器问题,未能连接到网络原因

    通过打开各种浏览器,发现只有IE不能上网,QQ之类的都能上网,不能登入LOL 只有IE是出现:远程计算机或设备将不接受连接 这个问题 解决办法是: 1.win+r --> 输入regedit 打 ...

  8. SparkStreaming整合Flume的pull方式之启动报错解决方案

    Flume配置文件: simple-agent.sources = netcat-source simple-agent.sinks = spark-sink simple-agent.channel ...

  9. Area in Triangle /// oj10229

    题目大意: 给出三角形的三个顶点 再给一条绳(绳长不超过三角形周长) 求绳子在三角形中能围出的最大面积 题解链接 http://blog.sina.com.cn/s/blog_6a46cc3f0100 ...

  10. lucene入门-搜索方式

    1 package com.home.utils; import java.util.ArrayList; import java.util.List; import org.apache.lucen ...