API后台控制器示例(分页 添加)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using DAL;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using DAL;
namespace .Shopping.Controllers
{
public class ShoppingController : ApiController
{
ShoppingDAL dal = new ShoppingDAL();
/// <summary>
/// 分页查询优惠券领取的方法
/// </summary>
/// <param name="pageSize">每页记录数</param>
/// <param name="currentPage">当前第几页</param>
/// <param name="yhfs">优惠方式</param>
/// <param name="hxzt">核销状态</param>
/// <param name="mc">优惠券名称</param>
/// <returns></returns>
public pageModel Get(int pageSize = 3, int currentPage = 1, string yhfs = "", string hxzt = "", string mc = "")
{
//修正传入的页数<1的情况
if(currentPage < 1)
{
currentPage = 1;
}
//查询出所有列表
var list = dal.Show();
{
public class ShoppingController : ApiController
{
ShoppingDAL dal = new ShoppingDAL();
/// <summary>
/// 分页查询优惠券领取的方法
/// </summary>
/// <param name="pageSize">每页记录数</param>
/// <param name="currentPage">当前第几页</param>
/// <param name="yhfs">优惠方式</param>
/// <param name="hxzt">核销状态</param>
/// <param name="mc">优惠券名称</param>
/// <returns></returns>
public pageModel Get(int pageSize = 3, int currentPage = 1, string yhfs = "", string hxzt = "", string mc = "")
{
//修正传入的页数<1的情况
if(currentPage < 1)
{
currentPage = 1;
}
//查询出所有列表
var list = dal.Show();
if (!string.IsNullOrEmpty(yhfs)) //如果传入的优惠方式不为空,则将列表中对优惠方式进行过滤
{
list = list.Where(s => s.yhfs == yhfs).ToList();
}
if (!string.IsNullOrEmpty(hxzt)) //过滤核销状态
{
list = list.Where(s => s.hxzt == hxzt).ToList();
}
if (!string.IsNullOrEmpty(mc)) //过滤优惠券名称(模糊查询)
{
list = list.Where(s => s.mc.Contains(mc)).ToList();
}
var count = list.Count(); //查询出总记录数
//查询出总页数
int page;
if(count % pageSize == 0)
{
page = count / pageSize;
}
else
{
page = count / pageSize + 1;
}
//纠正当前页 > 总页数的情况
if(currentPage > page)
{
currentPage = page;
}
//使用linq分页查询列表
list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
{
list = list.Where(s => s.yhfs == yhfs).ToList();
}
if (!string.IsNullOrEmpty(hxzt)) //过滤核销状态
{
list = list.Where(s => s.hxzt == hxzt).ToList();
}
if (!string.IsNullOrEmpty(mc)) //过滤优惠券名称(模糊查询)
{
list = list.Where(s => s.mc.Contains(mc)).ToList();
}
var count = list.Count(); //查询出总记录数
//查询出总页数
int page;
if(count % pageSize == 0)
{
page = count / pageSize;
}
else
{
page = count / pageSize + 1;
}
//纠正当前页 > 总页数的情况
if(currentPage > page)
{
currentPage = page;
}
//使用linq分页查询列表
list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
var m = new pageModel();
m.list = list; //列表就是分页查询出的列表
m.totalCount = count;
m.totalPage = page;
m.currentPage = currentPage;
return m;
}
m.list = list; //列表就是分页查询出的列表
m.totalCount = count;
m.totalPage = page;
m.currentPage = currentPage;
return m;
}
// POST: api/Shopping
/// <summary>
/// 添加优惠券的控制器
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Post([FromBody]yhqModel m)
{
//如果使用时间限制选择第一个选项
if(m.sysjCheck == 1)
{
m.ksrq = DateTime.Now;
m.jsrq = DateTime.Now.AddDays(m.sysjAdd);
}
/// <summary>
/// 添加优惠券的控制器
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Post([FromBody]yhqModel m)
{
//如果使用时间限制选择第一个选项
if(m.sysjCheck == 1)
{
m.ksrq = DateTime.Now;
m.jsrq = DateTime.Now.AddDays(m.sysjAdd);
}
return dal.Add(m);
}
}
}
}
}