.Net CoreDBHelper實現數據列表顯示


API代碼
using
System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace IOT.Month5.API.Controllers { [EnableCors("any")] [Route("goods")] [ApiController] public class GoodsController : ControllerBase { BLL.BaseBLL bll = new BLL.BaseBLL(); List<Model.JiLuModel> list = new List<Model.JiLuModel>(); [Route("list")] [HttpGet] public List<Model.JiLuModel> GetJiLuModels(string name) { list = bll.GetJiLuModels(name); return list; } [Route("add")] [HttpPost] public int Add(Model.YouHuiModel model) { int code = 0; code = bll.Add(model); if(code > 0) { //添加成功 return 1; } else { //添加不成功返回-1 return -1; } } } }

BLL 代碼

using System;
using System.Collections.Generic;
using System.Data;
using Model;
using Newtonsoft.Json;

namespace BLL
{
    public class BaseBLL
    {
        DAL.DBHelper Helper = new DAL.DBHelper();
        /// <summary>
        /// 顯示領取記錄表並按領取時間排序
        /// </summary>
        /// <returns></returns>
        public List<Model.JiLuModel> GetJiLuModels(string name)
        {
            string sql = "select u.User_Name,y.You_Name,y.You_Type,y.You_TiaoJian,y.ID,j.CreateDate,j.IsUp from Base_jilu j join Base_User u on u.ID = j.User_ID join Base_YouHui y on y.ID = j.You_ID where 1=1 ";
            //優惠券名稱查詢
            if(!string.IsNullOrEmpty(name))
            {
                sql += $" and You_Name like '%{name}%' order by j.CreateDate desc";
            }
            DataTable table = Helper.ExecSql(sql);
            //轉成Json對象
            string strJson = JsonConvert.SerializeObject(table);
            //轉為List
            List<Model.JiLuModel> list = JsonConvert.DeserializeObject<List<Model.JiLuModel>>(strJson);
            return list;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.YouHuiModel model)
        {
            string sql = $"insert into Base_YouHui values ({Guid.NewGuid()},'{model.You_Name}','{model.You_Type}','{model.You_TiaoJian}','{model.You_Fangshi}','{model.You_Number}','{model.You_Lei}')";
            return Helper.ExecuteNonQuery(sql);
        }
    }
}

 

cshtml

@{
    ViewData["Title"] = "Index";
}
<script src="~/lib/jquery/dist/jquery.js"></script>
<h1>記錄表顯示</h1>
<a href="#" onclick="tian()" style="margin-left:500px">新增優惠券</a>
<div>
    <div>
        <input type="text" placeholder="搜索優惠券" id="chaName" /> <a href="#" id="Search">🔍</a>
    </div>
    <table class="layui-table">
        <tr>
            <th>用戶昵稱</th>
            <th>優惠券名稱</th>
            <th>優惠券類型</th>
            <th>使用門檻</th>
            <th>優惠額度</th>
            <th>優惠券編號</th>
            <th>領取時間</th>
            <th>核銷狀態</th>
        </tr>
        <tbody id="tb"></tbody>
    </table>
</div>
<script>
    var tr = "";
    function ReLoad() {
        $('#tb').empty();
        $.get('http://localhost:53468/goods/list?name=' + $('#chaName').val(), function (data) {
            $.each(data, function (index, item) {
                $('#tb').empty();
                tr += '<tr><td>' + item.user_Name + '</td>';
                tr += '<td>' + item.you_Name + '</td>';
                tr += '<td>' + item.you_Type + '</td>';
                tr += '<td>' + item.you_TiaoJian + '</td>';
                tr += '<td>' + 3.00 + '</td>';
                tr += '<td>' + item.you_ID + '</td>';
                tr += '<td>' + item.createDate + '</td>';
                tr += '<td>' + (item.isUp == "0" ? "未核銷" : "已核銷") + '</td></tr>';
            })
            $('#tb').append(tr);
        });
    }
    //點擊查詢優惠券名稱
    $('#Search').click(function () {
        $('#tb').empty();
        var name = $('#chaName').val();
        ReLoad(name);
    });
    //點擊添加跳轉到添加頁面
    function tian() {
        window.location.href = 'shopping/add';
    }
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM