猿问

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

首先申明个人做的项目是采用传统的.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");
        }

大家帮帮忙吧

POPMUISE
浏览 360回答 11
11回答

繁星淼淼

用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

ITMISS

分页推荐使用Jquery控件

白板的微信

能提供点不 大哥

幕布斯7119047

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;        }

一只萌萌小番薯

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

Smart猫小萌

@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--;//上翻一页。 } 大概就这意思吧。希望对你有帮助。

HUWWW

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

杨魅力

un.list = CustomerInfo.GetModel() as List<DbCon_view>;un.list.skip(i-1*10).take(10)

慕神8447489

恩 我就是看这个解决了
随时随地看视频慕课网APP
我要回答