天天看點

.Net core Api背景擷取資料,異步方法中,資料需采用Linq分頁

.net core api

.Net core Api背景擷取資料,異步方法中,資料需采用Linq分頁
.Net core Api背景擷取資料,異步方法中,資料需采用Linq分頁
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

.Net core Api背景擷取資料,異步方法中,資料需采用Linq分頁
<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>