.Net core Api后台获取数据,异步方法中,数据需采用Linq分页

.net core api

复制代码
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Caching.Memory;
using Weeko3_Test.BLL.IBLL;
using Weeko3_Test.Model;

namespace Weeko3_Test.Api.Controllers
{
    [Route("api/Product")]
    [ApiController]
    [EnableCors("any")]
    public class ProductController : ControllerBase
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly IProductBll _bll;
        
        public ProductController(IProductBll bll)
        {
            _bll = bll; 
        }
        [Route("SelectList")]
        [HttpGet]
        ////[EnableCors("any")]
        public async Task<PageViewModel> SelectList(int pageIndex = 1, int pageSize = 5)
        {
            List<ProductInfoModel> list = await Task.Run(() => { return _bll.Select(); });
            int count = list.Count;
            var v = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            PageViewModel model = new PageViewModel();
            model.PageTotal = int.Parse(Math.Ceiling(decimal.Parse(count.ToString()) / pageSize).ToString());
            model.Models = v.ToList();
            return model;
        }
        
    }
}
View Code
复制代码

cshtml

复制代码
<link href="~/css/site.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.js"></script>


<table class="layui-table" lay-size="sm">
    <colgroup>
        <col width="150">
        <col width="200">
        <col width="200">
        <col width="200">
        <col width="200">
    </colgroup>
    <thead>
        <tr>
            <th>产品名称</th>
            <th>投资金额</th>
            <th>投资收益</th>
            <th>投资时间</th>
            <th>投资编号</th>
        </tr>
    </thead>
    <tbody id="tb">
    </tbody>
</table>
<div>
    <div>
        <input id="btn_First" type="button" value="首页" />
        <input id="btn_Pro" type="button" value="上一页" />
        <input id="btn_Next" type="button" value="下一页" />
        <input id="btn_Last" type="button" value="尾页" />

    </div>
</div>
<script>
    var pageSize = 5;
    var pageIndex = 1;
    var count = 0;
    $(function () {
        Show();
    })
    function Show() {
        $.ajax({
            url: "http://localhost:51457/api/product/SelectList",
            type: "get",
            dataType: "json",
            data: { pageIndex: pageIndex, pageSize: pageSize },
            success: function (data) {
                count = data.pageTotal;
                $('#tb').empty();
                //循环遍历
                $.each(data.models, function (index, item) {
                    //定义变量
                    var tr = '';
                    //开始拼接
                    tr += '<tr class="active">';
                    tr += '<td>' + item.product_Name + '</td>';
                    tr += '<td>' + item.startMoney + '' + '</td> ';
                    tr += '<td>' + item.produce_ShouYi + '' + '</td>';
                    tr += '<td>' + item.tCreateDate + '</td> ';
                    tr += '<td>' + item.product_No + '</td>';
                    tr += '</tr>';
                    //尾部添加
                    $('#tb').append(tr);
                })
            }
        });
    }



    //首页
    $('#btn_First').click(function () {
        pageIndex = 1;
        Show();

    })
    //上一页
    $('#btn_Pro').click(function () {
        if (pageIndex - 1 < 1) {
            pageIndex = 1;
            alert("到顶了");
        } else {
            pageIndex--;
            Show();
        }

    })
    //下一页
    $('#btn_Next').click(function () {
        if (pageIndex + 1 > count) {
            pageIndex = count;
            alert("到底了");
        } else {
            pageIndex++;
            Show();
        }
    })
    //尾页
    $('#btn_Last').click(function () {
        pageIndex = count;
        Show();
    })

</script>
View Code
复制代码

 

posted on   独行者*  阅读(578)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示