我的分页控件算法,稍有点简单

public  ActionResult Index( string  count, string  current, string  css)
        {
            ViewData[
" Message " =   " Welcome to ASP.NET MVC! " ;

            
int  allCount  =   int .Parse(count); // 总条数
             int  currentPage  =   int .Parse(current); // 当前页
            StringBuilder pager  =   new  StringBuilder();

            
int  page  =  (allCount  %  pageSize  >   0 ?  allCount  /  pageSize  +   1  : allCount  /  pageSize;
            
if  (page  >   1 )
            {
                pager.Append(
" <label><a href='/'>首页</a></label> " );
                
                
if  (page  >=   2   &&  page  <=   10 )
                {
                    
for  ( int  i  =   1 ; i  <=  page; i ++ )
                    {
                        
if  (i  ==  currentPage)
                            pager.Append(
" <label class=' "   +  css  +   " '><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                        
else
                            pager.Append(
" <label><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                    }
                }
                
else
                {
                    
if  (currentPage  <=   4 )
                    {
                        
for  ( int  i  =   1 ; i  <=   4 ; i ++ )
                        {
                            
if  (i  ==  currentPage)
                                pager.Append(
" <label class=' "   +  css  +   " '><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                            
else
                                pager.Append(
" <label><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                        }
                        pager.Append(
" <label>...</label> " );
                    }
                    
else   if  (currentPage  >   3   &&  currentPage  <  page - 3  )
                    {
                        
for  ( int  i  =   1 ; i  <=   2 ; i ++ )
                        {
                                pager.Append(
" <label><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                        }
                        pager.Append(
" <label>...</label> " );

                        pager.Append(
" <label><a href='/'>第 "   +  (currentPage  -   1 ).ToString()  +   " 页</a></label> " );
                        pager.Append(
" <label class=' "   +  css  +   " '><a href='/'>第 "   +  currentPage.ToString()  +   " 页</a></label> " );
                        pager.Append(
" <label><a href='/'>第 "   +  (currentPage  +   1 ).ToString()  +   " 页</a></label> " );

                        pager.Append(
" <label>...</label> " );
                        
for  ( int  i  =  page  -   1 ; i  <=  page; i ++ )
                        {
                                pager.Append(
" <label><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                        }
                    }
                    
else
                    {
                        pager.Append(
" <label>...</label> " );
                        
for  ( int  i  =  page  -   3 ; i  <=  page; i ++ )
                        {
                            
if  (i  ==  currentPage)
                                pager.Append(
" <label class=' "   +  css  +   " '><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                            
else
                                pager.Append(
" <label><a href='/'>第 "   +  i.ToString()  +   " 页</a></label> " );
                        }
                    }
                }
                pager.Append(
" <label><a href='/'>末页</a></label> " );
            }
            ViewData[
" all " =  count;

            ViewData[
" current " =  current;
            ViewData[
" pageString " = MvcHtmlString.Create( pager.ToString());
            
return  View();
        }

你可能感兴趣的:(算法)