In the past, when I used Asp.net, I always customized a base class to inherit the page class to implement some common operations on the page to reduceCode. Asp.net MVC 2 does not have the paging function. Implement the paging function in the base class. define common paging variables, such as recordcount, pagesize, and curpage, on the page. The pagination string is assigned to viewdata ["page"] Through getpager (), and viewdata ["page"] is directly output to the view page.
Instance code basecontroller. CS
1 Public Class Basecontroller: Controller
2 {
3 // LINQ data operation type DB
4 Public DB = New DB ();
5 Public Int Recordcount = 0 ;
6 Public Int Pagesize = 10 ;
7
8 Public Int Curpage
9 {
10 Get
11 {
12 Return Dntrequest. getint ( " Page " , 1 );
13 }
14 }
15 Public Void Getpager ()
16 {
17 Int Pagecount = ( Int ) Math. Ceiling (recordcount / (( Double ) Pagesize ));
18 Viewdata [ " Page " ] = Utils. getpagenumbers (curpage, pagecount, geturl (), 5 );
19 }
20 Public String Geturl ()
21 {
22 String URL = Request. url. tostring ();
23 URL = URL. substring (URL. lastindexof ( " / " ) + 1 );
24
25 RegEx Reg = New RegEx ( @" & Page = ([0-9] +) " , Regexoptions. ignorecase );
26 Match m = Reg. Match (URL );
27 If (M. Success)
28 {
29 URL = URL. Replace (M. Groups [ 0 ]. Value, "" );
30 }
31 Reg = New RegEx ( @" \? Page = ([0-9] +) " , Regexoptions. ignorecase );
32 M = Reg. Match (URL );
33 If (M. Success)
34 {
35 URL = URL. Replace (M. Groups [ 0 ]. Value, " ? 1 = 1 " );
36 }
37 Return URL;
38 }
39 }
Articlecontroller. CS
Code
Public Class Articlecontroller: basecontroller
{
Protected Override Void Onresultexecuting (resultexecutingcontext filtercontext)
{
VaR Cate = DB. Category. tolist ();
Cate. insert ( 0 , New Category () {ID = 0 , Name = " Not Set " });
Viewdata [ " Category " ] = Cate;
Base . Onresultexecuting (filtercontext );
}
PublicActionresult index (article art)
{
Viewdata [ " Searchmodel " ] = Art;
VaR article = DB. Find < Article > (ART );
VaR list = DB. Article. Where (d => D. Title. Contains ( " 22 " ));
Recordcount = Article. Count ();
Article = Article. Skip (curpage - 1 ) * Pagesize). Take (pagesize );
Getpager ();
Return View ( " Index " , Article );
}
// Public actionresult index ()
// {
// Viewdata ["searchmodel"] = new article ();
// VaR article = from D in db. Article
// Select D;
// Recordcount = article. Count ();
// Article = article. Skip (CurPage-1) * pagesize). Take (pagesize );
// Getpager ();
// Return view ("Index", article );
// }
Public Actionresult add ()
{
Article art = New Article ();
Art. createtime = Datetime. now;
Return View (ART );
}
[Httppost]
[Validateinput ( False )]
Public Actionresult add (article art)
{
Try
{
If ( This . Modelstate. isvalid)
{
DB. Article. insertonsubmit (ART );
DB. submitchanges ();
Return Redirecttoaction ( " Index " );
}
Else
{
Return View (ART );
}
}
Catch (Exception E)
{
Return Content (E. Message );
}
}
Public Actionresult edit ( Int ID)
{
Return View (db. Article. firstordefault (d => D. id = ID ));
}
[Httppost]
[Validateinput ( False )]
Public Actionresult edit ( Int ID, article art1)
{
Try
{
Article art = DB. Article. firstordefault (d => D. id = ID );
Updatemodel (ART );
DB. submitchanges ();
Return This . Redirecttoaction ( " Index " );
}
Catch
{
Return View (art1 );
}
}
Public Actionresult Delete ( Int ID)
{
DB. Article. deleteonsubmit (db. Article. firstordefault (d => D. id = ID ));
DB. submitchanges ();
Return Redirecttoaction ( " Index " );
}
}