首页 新闻 会员 周边 捐助

MVC分页 的问题。大侠们路过的帮帮忙!

0
悬赏园豆:20 [已解决问题] 解决于 2012-07-17 14:55

首先申明个人做的项目是采用传统的.net技术访问数据库,不用linq 也没有用什么数据库模型。

控制器代码:

        public ActionResult Index(int? id = 1)
        {
            unite un = new unite();
            un.dt = CustomerInfo.GetList().Tables[0];
            un.list = CustomerInfo.GetModel() as List<DbCon_view>;

            if (un.dt.Rows.Count> 0)
            {
                return View(un);
            }
            return View("Default");
        }

大家帮帮忙吧

s_p的主页 s_p | 初学一级 | 园豆:140
提问于:2012-07-16 17:13
< >
分享
最佳答案
0

用SQL的话,我给你一个存储过程吧,我们一直用..经过无数次考验的..

你在写一个方法来调用这个存储过程就好了..

 1 Create proc SP_Page
 2 @QueryStr nvarchar(max),
 3 @OrderBy nvarchar(100),
 4 @PageIndex INT,
 5 @PageSize int,
 6 @RowCount INT OUTPUT
 7 AS
 8 BEGIN
 9     declare @sqlstring nvarchar(max),@sqlCount int,@pagebegin nvarchar(15),@pageend nvarchar(15)
10             --查询总量,开始页数,结束页数,
11             
12     --开始页数        
13     set @pagebegin=CAST(((@PageIndex-1)*@PageSize+1) as nvarchar(15))
14     --结束页数
15     set @pageend=CAST((@PageIndex*@PageSize) as nvarchar(15))
16     
17     SET @sqlstring='select * ,Row_Number()over(order by '+@OrderBy +' desc ) as rowNum from ( '+@QueryStr +' ) as tem ' 
18     
19     declare @tStr nvarchar(max)
20     set  @tStr = 'select @RowCount = Count(0) from ('+@sqlstring+') as counts '
21     
22     EXEC SP_EXECUTESQL @tStr,N'@RowCount INT OUTPUT',@RowCount OUTPUT
23     
24     set @sqlstring='select * from ( '+@sqlstring+' ) as endResult where rowNum between '+@pagebegin+' and '+@pageend    
25     exec(@sqlstring)
26 END
收获园豆:8
hexllo | 菜鸟二级 |园豆:318 | 2012-07-16 17:37

恩 这个方法怎么调用啊 我学的是oracle的语法所以对sql的不是很熟?

s_p | 园豆:140 (初学一级) | 2012-07-16 17:44

@s_p: 你现在如果还是用Oracle的话我估计这个存储过程不一定能运行正确哦..

调用它的方法...给你一个C#代码示范吧..

 1  public DataTable GetAllData(int PageIndex, int PageSize, out int RowCount)
 2         {
 3             string queryStr = "select * from abc";
 4             RowCount = 0;
 5             string cmdStr = "SP_Page";
 6             string OrderByColumnName = "CreateTime";
 7             SqlParameter[] param = new SqlParameter[] 
 8                 {
 9                     new SqlParameter ("@QueryStr",queryStr ),
10                     new SqlParameter ("@OrderBy",OrderByColumnName ),   
11                     new SqlParameter("@PageIndex",PageIndex),
12                     new SqlParameter("@PageSize",PageSize),
13                     new SqlParameter("@RowCount",RowCount)
14                 };
15             param[4].Direction = ParameterDirection.Output;
16 
17             DataTable dt = base.ExecuteQueryAsDataTable(cmdStr, CommandType.StoredProcedure, param);
18             RowCount = Convert.ToInt32(param[4].Value);
19             return dt;
20         }
hexllo | 园豆:318 (菜鸟二级) | 2012-07-16 17:53

@hexllo: 谢谢了 我用了其他方法解决  了

s_p | 园豆:140 (初学一级) | 2012-07-17 14:54
其他回答(5)
0

分页推荐使用Jquery控件

收获园豆:2
CrazyJinn | 园豆:799 (小虾三级) | 2012-07-16 17:28
0

就按 Ado.net 来分页,分页方法写了没,只是在Action中调用。

CustomerInfo.GetList() //传页码,传记录数,返回总记录数,等其他参数。
收获园豆:4
Qlin | 园豆:2403 (老鸟四级) | 2012-07-16 17:33

能提供点不 大哥

支持(0) 反对(0) s_p | 园豆:140 (初学一级) | 2012-07-16 17:41
0

DataSet方法实现分页自己调试下:这里的dtSource 是把数据放到一张表里面,number是你分页的时候每页要显示多少条数据.把里面的字段自己对照自己的数据库更改下。注意返回的DataSet.用DataSet中的Tables.Count来作为总的页数.每页做为数据源,其他的就靠你自己了。

public DataSet setDataPage(DataTable dtSource, int number)
        {
            DataSet dsTarget = new DataSet();
            DataTable dts = null;
            int j = 0;
            if (dtSource.Rows.Count <= number)
            {
                dts = new DataTable();
                dsTarget.Tables.Add(dtSource);
            }
            else
            {
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    int a = dtSource.Rows.Count / number;
                    int b = dtSource.Rows.Count % number;
                    DataRow dr = null;
                    if (i <= a * number - 1)
                    {
                        if (dts == null)
                        {
                            dts = new DataTable();
                            dts.Columns.Add("LEAVEID");
                            dts.Columns.Add("USERID");
                            dts.Columns.Add("BACKID");
                            dts.Columns.Add("LOCATION");
                            dts.Columns.Add("CONTENT");
                            dts.Columns.Add("SIGN");
                            dts.Columns.Add("TYPE");
                            dts.Columns.Add("COLOR");
                            dts.Columns.Add("LEAVETIME");
                            dts.Columns.Add("FID");
                        }
                        dr = dts.NewRow();
                        dr["LEAVEID"] = dtSource.Rows[i]["LEAVEID"];
                        dr["USERID"] = dtSource.Rows[i]["USERID"];
                        dr["BACKID"] = dtSource.Rows[i]["BACKID"];
                        dr["LOCATION"] = dtSource.Rows[i]["LOCATION"];
                        dr["CONTENT"] = dtSource.Rows[i]["CONTENT"];
                        dr["SIGN"] = dtSource.Rows[i]["SIGN"];
                        dr["TYPE"] = dtSource.Rows[i]["TYPE"];
                        dr["COLOR"] = dtSource.Rows[i]["COLOR"];
                        dr["LEAVETIME"] = dtSource.Rows[i]["LEAVETIME"];
                        dr["FID"] = dtSource.Rows[i]["FID"];
                        dts.Rows.Add(dr);
                        j++;
                        if (j == number)
                        {
                            dsTarget.Tables.Add(dts);
                            j = 0;
                            dts = null;
                        }
                    }
                    if (i > a * number - 1)
                    {
                        if (dts == null)
                        {
                            dts = new DataTable();
                            dts.Columns.Add("LEAVEID");
                            dts.Columns.Add("USERID");
                            dts.Columns.Add("BACKID");
                            dts.Columns.Add("LOCATION");
                            dts.Columns.Add("CONTENT");
                            dts.Columns.Add("SIGN");
                            dts.Columns.Add("TYPE");
                            dts.Columns.Add("COLOR");
                            dts.Columns.Add("LEAVETIME");
                            dts.Columns.Add("FID");
                        }
                        dr = dts.NewRow();
                        dr["LEAVEID"] = dtSource.Rows[i]["LEAVEID"];
                        dr["USERID"] = dtSource.Rows[i]["USERID"];
                        dr["BACKID"] = dtSource.Rows[i]["BACKID"];
                        dr["LOCATION"] = dtSource.Rows[i]["LOCATION"];
                        dr["CONTENT"] = dtSource.Rows[i]["CONTENT"];
                        dr["SIGN"] = dtSource.Rows[i]["SIGN"];
                        dr["TYPE"] = dtSource.Rows[i]["TYPE"];
                        dr["COLOR"] = dtSource.Rows[i]["COLOR"];
                        dr["LEAVETIME"] = dtSource.Rows[i]["LEAVETIME"];
                        dr["FID"] = dtSource.Rows[i]["FID"];
                        dts.Rows.Add(dr);
                        if (i == dtSource.Rows.Count - 1)
                        {
                            dsTarget.Tables.Add(dts);
                        }
                    }
                }
            }
            return dsTarget;
        }

收获园豆:2
Bj-Soctt-胖仔 | 园豆:70 (初学一级) | 2012-07-16 18:09

恩 好的谢谢 我去试试对了在麻烦你下了  假如上面的都运行正确,对应的视图那个html代码怎么写啊 就是分页的那一块 原谅我是初学者.

支持(0) 反对(0) s_p | 园豆:140 (初学一级) | 2012-07-16 18:20

@s_p: 不知道你怎么表现分页的。简单的方法在TextBox两边加个<<或者>>用个超链接控件触发个click事件。里面写简单的处理就ok了.

DataSet  st=setDataPage(dtsource,10);

currPage=0;//当前页面。

PreQuery_Click(object sender,EventArgs e)//上一页

{

 

if(st.Tables.Count<0)

{

//你要执行的操作。

}

if(st.Tables.Count==1)

//当前只有一页,你要执行的操作

currPage--;//上翻一页。

}

大概就这意思吧。希望对你有帮助。

支持(0) 反对(0) Bj-Soctt-胖仔 | 园豆:70 (初学一级) | 2012-07-17 09:24

@Bj-Soctt-胖仔: 大哥这个是mvc mvc里面没有事件了 我解决了 mvc彻底了抛弃了控件 和双击

支持(0) 反对(0) s_p | 园豆:140 (初学一级) | 2012-07-17 10:30
0
  un.list = CustomerInfo.GetModel() as List<DbCon_view>;
un.list.skip(i-1*10).take(10)
收获园豆:4
chenping2008 | 园豆:9836 (大侠五级) | 2012-07-16 19:32
0

MVC 分页控件可以参考 http://www.webdiyer.com/controls/mvcpager

guddujianxiao | 园豆:204 (菜鸟二级) | 2012-07-17 12:05

恩 我就是看这个解决了

支持(0) 反对(0) s_p | 园豆:140 (初学一级) | 2012-07-17 14:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册