mvc+webservice+儲存過程分頁
1.建立資料庫
create database Persion
2.建立資料表
create table Student(
id int primary key identity,
Name varchar(50),
Pass varchar(50)
)
3.往表中插入資料
insert into Student values('美圖','xiuxiu')//可以多插入幾行
4.建立儲存過程的分頁
create proc usp_Page
@pagesize int=2,
@pageindex int=1,
@recordcount int output,
@pagecount int output
as
begin
select
t.id,
t.Name,
t.Pass
from(select *,rn=row_number() over(order by id asc) from Student) as t
where t.rn between (@pageindex-1)*@pagesize+1 and @pagesize*@pageindex
--計算總的記錄條數
set @recordcount=(select count(*) from Student)
--計算總頁數
set @pagecount=ceiling(@recordcount*1.0/@pagesize)
end
5.webservice呼叫分頁
(1).返回分頁資料
public DataTable PageShow(int pageIndex = 1, int pageSize = 2)
{
string consstr = "Data Source=.;Initial Catalog=Persion;Integrated Security=True";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(consstr))
{
using (SqlCommand cmd = new SqlCommand("usp_Page", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pageSize;
cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = pageIndex;
cmd.Parameters.Add("@recordcount", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@pagecount", SqlDbType.Int).Direction = ParameterDirection.Output;
using (SqlDataAdapter dta = new SqlDataAdapter(cmd))
{
dta.Fill(dt);
return dt;
}
}
}
}
(2)返回分頁行數
string count;
public string Count(int pageIndex=1,int pageSize=2)
{
string consstr = "Data Source=.;Initial Catalog=Persion;Integrated Security=True";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(consstr))
{
using (SqlCommand cmd = new SqlCommand("usp_Page", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pageSize;
cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = pageIndex;
cmd.Parameters.Add("@recordcount", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@pagecount", SqlDbType.Int).Direction = ParameterDirection.Output;
using (SqlDataAdapter dta = new SqlDataAdapter(cmd))
{
dta.Fill(dt);
count = cmd.Parameters["@recordcount"].Value.ToString();
return count;
}
}
}
}
6.mvc呼叫webservice
public ActionResult Index(int pageIndex = 1, int pageSize = 2)
{
ViewData["pageIndex"] = pageIndex;
ViewData["pageSize"] = pageSize;
ViewData["total"] = int.Parse(page.Count(pageIndex,pageSize));
DataTable dt = page.PageShow(pageIndex,pageSize);
var linq = from b in dt.AsEnumerable()
select new Student()
{
id = b.Field<int>("id"),
Name = b.Field<string>("Name"),
Pass = b.Field<string>("Pass"),
};
return View(linq.ToList());
}
7.前臺分頁類
namespace System.Web.Mvc
{
public static class PageHtml
{
//輸出分頁的超級連結標籤
//自定義分頁的helper的擴充套件
public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper,int currentPage,int pageSize,int totalCount)
{
//路徑
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == 0 ? 3 : pageSize;
var totalPages=Math.Max((totalCount+pageSize-1)/pageSize,1);//總頁數
var output = new StringBuilder();
if (totalPages > 1)
{
{
//處理首頁連線
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首頁</a>", redirectTo, pageSize);
}
if (currentPage > 1)
{
//處理上一頁連線
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一頁</a>", redirectTo,currentPage-1,pageSize);
}
else
{
}
output.Append("");
int currint = 5;
for (int i = 0; i <=10; i++)
{
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{
//當前頁處理
output.AppendFormat("<a class='cpd' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>", redirectTo,currentPage, pageSize,currentPage);
}
else
{
//一般頁處理
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>", redirectTo, currentPage+i-currint,pageSize, currentPage+i-currint);
}
}
output.Append("");
}
if (currentPage < totalPages)
{
//處理下一頁的連線
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一頁</a>", redirectTo, currentPage+1, pageSize);
}
output.Append("");
if (currentPage != totalPages)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末頁</a>", redirectTo, totalPages, pageSize);
}
output.Append("");
}
output.AppendFormat("第{0}頁/共{1}頁", currentPage, totalPages);//統計
return new HtmlString(output.ToString());
}
}
}
8.前臺頁面
@model IEnumerable<Text.Models.Student>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Pass)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Pass)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.id }) |
@Html.ActionLink("Details", "Details", new { id = item.id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.id })
</td>
</tr>
}
</table>
<div id="pageNam">
@Html.ShowPageNavigate((int)ViewData["pageIndex"], (int)ViewData["pageSize"], (int)ViewData["total"])
</div>
</body>
</html>
已經測試過,全部可用