API的控制器

DAL

/// <summary>
/// 万能
/// </summary>
/// <param name="Pageindex"></param>
/// <param name="Pagesize"></param>
/// <returns></returns>
public PageList<stuModel> Getlist2(int pageindex, int pagesize,string where="")
{
var Totcounts = 0;
//var pp = db.GetpageList(Pageindex, Pagesize,out Totcount);

SqlCommand cmd = new SqlCommand("Proc_stu", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int)).Value = pageindex;

cmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int)).Value = pagesize;
cmd.Parameters.Add(new SqlParameter("@order", SqlDbType.NVarChar, 100)).Value = "id";
cmd.Parameters.Add(new SqlParameter("@totName", SqlDbType.NVarChar, 100)).Value = "stu";
var name = "";
if (!string.IsNullOrWhiteSpace(where))//判断
{
name += " where 1=1 and name like ''%" + where + "%'' ";
}
//if (type > 0)//判断
// name += " AND s.TypeID=" + type;

cmd.Parameters.Add(new SqlParameter("@where", SqlDbType.NVarChar, 1000)).Value = name;

cmd.Parameters.Add(new SqlParameter("@Tocount", SqlDbType.Int));
//参数
cmd.Parameters["@Tocount"].Direction = ParameterDirection.Output;

SqlDataAdapter dr = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
dr.Fill(ds);
Totcounts = Convert.ToInt32(cmd.Parameters["@Tocount"].Value);//接收存储过程返回的总条数

 

var reust = JsonConvert.SerializeObject(ds);
var reust2 = JsonConvert.DeserializeObject<List<stuModel>>(reust);

PageList<stuModel> ss = new PageList<stuModel>();
ss.page = reust2;
ss.PageIndex = pageindex;
ss.PageSize = pagesize;
if (Totcounts % pagesize == 0)
{
ss.Tocount = Totcounts / pagesize;
}
else
{
ss.Tocount = Totcounts / pagesize + 1;
}
return ss;
}

BLL

public PageList<stuModel> Getlist2(int pageindex, int pagesize,string where = "")
{
return dal.Getlist2(pageindex,pagesize,where);
}

嵌套类

namespace Model
{
public class PageList<T>
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int Tocount { get; set; }

public List<T> page { get; set; }
}
}

API控制器

config.EnableCors();

using System.Web.Http.Cors;
 
[EnableCors("*","*","*")]
public class showApiController : ApiController
{
// GET: api/showApi
[HttpGet]
public PageList<stuModel> Get(int pageindex, int pagesize,string where = "")
{

var p= new BLL.stuBLL().Getlist2(pageindex,4,where);
return p;
}

mvc

<input id="Text1" type="text" />
<input id="Button1" type="button" value="查询" onclick="cha()" />
<div>
<table>
<tr>
<td>编号</td>
<td>名称</td>
<td>性别</td>
<td>年龄</td>
<td>操作</td>
</tr>
<tbody id="idname"></tbody>
</table>
</div>
<div id="pageHtml">

</div>
<script>
show(1, "pageHtml");

 

function show(pageindex,pageHtml,name)
{

debugger;
$.ajax({
url: "http://localhost:3778/api/showApi/",
type: "Get",
data: { pageindex: pageindex, pagesize: 4, where:name},
dataType: "json",
success: function (data) {
alert(data);

var str = "";
for (var i = 0; i < data.page.length; i++) {
str += "<tr>";
str += "<td>" + data.page[i].id + "</td>";
str += "<td>" + data.page[i].name + "</td>";
str += "<td>" + (data.page[i].sex == true ? "男" : "女") + "</td>";
str += "<td>" + data.page[i].age + "</td>";
str += "<td><input class='del' onclick='del(" + data.page[i].id + ")' type='button' value='删除' /></td>";
str += "<td><input class='upd' onclick='upd(" + data.page[i].id + ")' type='button' value='修改' /></td>";
str += "</tr>";
}
$("#idname").html(str);

// 没有页码的分页
//分页按钮
debugger;
var totalPage = data.Tocount
var page = "";
//计算上一页
var lastPage = pageindex < 2 ? 1 : pageindex - 1;
//计算下一页
var nextPage = pageindex == totalPage ? totalPage : pageindex + 1;

page += " 当前页:<span style=\"color:red;font-size:18px;\" >" + pageindex + "/" + totalPage + "</span>总页数";
page += " <span onclick=\"show(1)\">首页</span>";
page += " <span onclick=\"show(" + lastPage + ")\">上一页</span>";
page += " <span onclick=\"show(" + nextPage + ")\">下一页</span>";
page += " <span onclick=\"show(" + totalPage + ")\">尾页</span>";

$("#pageHtml").html(page);
}
})
}
function cha()
{
var name = $("#Text1").val();
show(1, "pageHtml",name);
}

posted on 2017-11-22 19:42  过客s  阅读(110)  评论(0编辑  收藏  举报