.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
原文地址:https://www.cnblogs.com/wj1107/p/13066743.html