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>

已经测试过,全部可用

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值