ASP.NET MVC 从入门到放弃(Day26)-PagedList分页套件介绍

接下来讲讲PagedList套件的使用方式

在前面有提到Entity Framework 使用的方式 这边以Day20资料库为例这次使用Order作为范例
https://ithelp.ithome.com.tw/upload/images/20210922/20140001oerg3XJL1A.png

1.首先先去工具->NuGet套件管理员->管理方案NuGet套件->安装PagedList.Mvc
https://ithelp.ithome.com.tw/upload/images/20210922/201400010L8tDfnoBX.png

2.新增一个PagedListController 并加入相关程序码

using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class PagedListController : Controller
    {
        // GET: PagedList
        private DBContext _db = new DBContext();

        protected override void Dispose(bool disposing)
        {          
            if (disposing)
            {
                _db.Dispose(); 
            }
            base.Dispose(disposing);

        }
        public ActionResult Index(int page = 1, int pageSize = 15)
        {
            var ListAll = from m in _db.Orders
                               select m;

            var pagelist= ListAll.ToList().ToPagedList(page, pageSize);

            return View(pagelist);
        }
    }
}

1.db 为连线资料库
2.page 为第几页
3.pageSize 显示几笔资料
4.最上方需加入using PagedList

3.View的部分

@model IEnumerable<WebApplication1.Models.Order>
@using PagedList.Mvc;
@using PagedList;

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CustomerID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.OrderDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RequiredDate)
        </th>

    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmployeeID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OrderDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.RequiredDate)
            </td>

            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
                @Html.ActionLink("Details", "Details", new { id = item.OrderID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
            </td>
        </tr>
    }

</table>
@Html.PagedListPager((IPagedList)Model, x => Url.Action("Index", new { page = x }))

1.上方需加入@using PagedList.Mvc、@using PagedList
2.最下方需加入@Html.PagedListPager

4.结果
https://ithelp.ithome.com.tw/upload/images/20210922/20140001kbQawQRRSv.png


<<:  [Day30]颁发和注销访问token

>>:  [Python 爬虫这样学,一定是大拇指拉!] DAY16 - 从爬虫看 API / CGI

如何自己设计一套ERP程序 前传-写ERP之前要决定的20件事 决定ERP主要编写程序语言

第2个决定 决定ERP主要编写程序语言 理论上所有程序语言只要能读取资料库的都可拿来设计一套ERP软...

DAY14 呼叫功能列表样板

@csrf_exempt def callback(request): if request.met...

css float

float称为浮动元素,可以想像是加上这个元素的物体都会飘浮在空中 接下来示范一些用法来认识这个元素...

Day30-结语

前言 今天就是铁人赛的最後一篇文章了,不免俗的要来写个总结来混一下篇幅XD,也希望这次的铁人赛可以让...

基础统计-认识基本图表与用途

不管是数据分析的过程还是分析结果的呈现,都推脱离不了「数据可视化」,将数据用图表呈现。然而我们该用哪...