首页 > Windows开发 > 详细

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

时间:2020-06-08 17:31:06      阅读:80      评论:0      收藏:0      [点我收藏+]

.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

 

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

原文:https://www.cnblogs.com/wj1107/p/13066743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!