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>