.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; } } }
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>
本文来自博客园,作者:独行者*,转载请注明原文链接:https://www.cnblogs.com/dotnetw/p/13066743.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?