在完成產品列表頁前要作一些準備功夫。首先是去下載MvcPager用了爲產品列表分頁。下載的多是基於MVC 2的,不要緊,能夠用在MVC 3上。若是有擔憂,下載源代碼從新編譯一次好了。下載後將DLL添加到引用裏。javascript
- routes . MapRoute (
- " Catalog " , // Route name
- " Catalog/List/{id}/{page} " , // URL with parameters
- new { controller = " Catalog " , action = " List " , page = 1 } // Parameter defaults
- ) ;
如今打開CatalogController.cs文件,在文件頭添加對MvcPager的引用,代碼以下:css
- using Webdiyer . WebControls . Mvc;
而後修改Index操做的代碼以下:java
- public ActionResult Index ( int? id )
- {
- PagedList < T_Products > q = dc . T_Products . OrderByDescending ( m = > m . CreateTime ) . ToPagedList ( id ?? 1 , 8 ) ;
- return View ( q ) ;
- }
代碼傳入的id是頁碼而不是產品分類編碼,這個要注意。由於要使用分頁,因此傳遞給視圖的是PagedList對象,而不是Queryable,這也是要注意的地方。於是,查詢結果須要經過toPagedList方法將Queryable對象轉換爲PagedList對象。第1個參數是當前頁面,若是id爲空,則默認爲第1頁。第2個參數是每頁顯示的產品數量,在這裏是8個。數據庫
在「Index」上單擊鼠標右鍵,選擇「添加視圖」( ,今天換了中文版,菜單也變了)。在視圖中添加如下代碼:ide
- @ using Webdiyer . WebControls . Mvc ;
- @ model PagedList < Extshop . Models . T_Products >
- @ {
- ViewBag . Title = " 產品列表 " ;
- PageData [ " id " ] = " " ;
- }
- < div class = " nav " >
- < a href = " @Url.Action( " " , " Catalog " ) " > 產品 < / a >
- < / div > < br / >
- < div id = " contentMain " style = " width:760px; " >
- < span class = " header " style = " width:750px; " > 產品列表 < / span >
- @ {
- foreach ( var c in Model )
- {
- < ul >
- < li > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > < img src = ' / Images / products / @ c . SamllImageUrl ' alt = " @c.Title " / > < / a > < / li >
- < li class = ' title ' > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > @ c . Title < / a > < / li >
- < li > 市場價: < del > @ c . MarketPrice . ToString ( " C " ) < / del > < / li >
- < li > 當前價: @ c . UnitPrice . ToString ( " C " ) < / li >
- < li > < span class = " rating-title " > 評 價: < / span >
- < div class = ' rating ' id = " @c.ProductID.ToString( " rat - 0 " ) " >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 1 " @ ( c . TotalRating = = 1 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 2 " @ ( c . TotalRating = = 2 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 3 " @ ( c . TotalRating = = 3 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 4 " @ ( c . TotalRating = = 4 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 5 " @ ( c . TotalRating = = 5 ? " checked='checked' " : " " ) / >
- < / div >
- < / li >
- < / ul >
- }
- }
- < div class = " clear " > < / div >
- < div class = ' pagenav ' > @ Html . Pager ( Model , new PagerOptions { PageIndexParameterName = " id " } ) < / div >
- < / div >
- < script type = " text/javascript " >
- jQuery ( function ( ) {
- $ ( " div .rating " ) . stars ( ) ;
- } ) ;
- < / script >
代碼第一句引用MvcPager,第二句定義接收的數據模型類型。由於是因此產品列表,因此PageData["id"]設置爲空字符串。其他代碼和首頁的差很少。第34句使用HTML方式顯示分頁代碼。這裏要注意的是PagerOptions選項中的PageIndexParameterName要設置爲id,由於操做中接收的當前頁參數是以id定義的。編碼
- 1 #contentMain .clear { clear : both ; }
- 2 #contentMain .pagenav { width : 760px ; text-align : center ; margin-bottom : 10px ; }
- 切換回CatalogController.cs文件,在Index操做下添加一個List操做,其代碼以下:
- 1 public ActionResult List(string id, int? page)
- 2 {
- 3 ViewData["id"] = id;
- 4 PagedList q = dc.T_Products
- 5 .Where(m => dc.T_Categories.Where(n => n.CategoryID.Substring(0, id.Length) == id).Select(n => n.CategoryID ).Contains(m.CategoryID))
- 6 .OrderByDescending(m => m.CreateTime).ToPagedList(page ?? 1, 4);
- 7 return View(q);
- 8 }
從代碼能夠看到,List操做有兩個參數,第1個是分類id,第2個是當前頁號。第3行經過ViewData["id"]向視圖傳遞當前的分類id,以更新左邊分類列表以及顯示導航條。其他的和Index操做差很少。如今爲List操做建立一個視圖,視圖內代碼以下:spa
- @ using Webdiyer . WebControls . Mvc ;
- @ model PagedList < Extshop . Models . T_Products >
- @ {
- ViewBag . Title = " 產品列表 " ;
- PageData [ " id " ] = ViewData [ " id " ] ;
- }
- < div class = " nav " >
- < a href = " @Url.Action( " " , " Catalog " ) " > 產品 < / a >
- @ { Html . RenderAction ( " Navbar " , " Catalog " , new { id = ViewData [ " id " ] } ) ; }
- < / div > < br / >
- < div id = " contentMain " style = " width:760px; " >
- < span class = " header " style = " width:750px; " > 產品列表 < / span >
- @ {
- foreach ( var c in Model )
- {
- < ul >
- < li > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > < img src = ' / Images / products / @ c . SamllImageUrl ' alt = " @c.Title " / > < / a > < / li >
- < li class = ' title ' > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > @ c . Title < / a > < / li >
- < li > 市場價: < del > @ c . MarketPrice . ToString ( " C " ) < / del > < / li >
- < li > 當前價: @ c . UnitPrice . ToString ( " C " ) < / li >
- < li > < span class = " rating-title " > 評 價: < / span >
- < div class = ' rating ' id = " @c.ProductID.ToString( " rat - 0 " ) " >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 1 " @ ( c . TotalRating = = 1 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 2 " @ ( c . TotalRating = = 2 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 3 " @ ( c . TotalRating = = 3 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 4 " @ ( c . TotalRating = = 4 ? " checked='checked' " : " " ) / >
- < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 5 " @ ( c . TotalRating = = 5 ? " checked='checked' " : " " ) / >
- < / div >
- < / li >
- < / ul >
- }
- }
- < div class = " clear " > < / div >
- < div class = ' pagenav ' > @ Html . Pager ( Model , new PagerOptions { PageIndexParameterName = " page " } ) < / div >
- < / div >
- < script type = " text/javascript " >
- jQuery ( function ( ) {
- $ ( " div .rating " ) . stars ( ) ;
- } ) ;
- < / script >
代碼第10行經過一個分部視圖顯示導航菜單。第35行要注意PageIndexParameterName是「page」,而不是Index視圖的「id」了。.net
下一步要作的是完成導航條的顯示。將文件切換回CatalogController.cs文件,添加一個名稱爲「Navbar」的子操做,其代碼以下:對象
- [ ChildActionOnly ]
- public ActionResult Navbar ( string id )
- {
- List < string > idlist = new List < string > ( ) ;
- idlist . Add ( id ) ;
- for ( int i = 0 ; i < ( id . Length - 2 ) ; i = i + 3 )
- {
- idlist . Add ( id . Substring ( 0 , i + 3 ) ) ;
- }
- var q = dc . T_Categories . Where ( m = > idlist . Contains ( m . CategoryID ) ) . OrderBy ( m = > m . CategoryID ) ;
- return PartialView ( q ) ;
- }
代碼首先獲取了當前分類的父類編號,而後從數據庫一次查詢出全部類別並排序。如今爲該子操做建立一個分部視圖,視圖代碼以下:排序
- @ model IEnumerable < Extshop . Models . T_Categories >
- @ {
- foreach ( var c in Model )
- {
- @ Html . Raw ( " >> " ) < a href = ' @ Url . Action ( " List " , " Catalog " , new { id = c . CategoryID } ) ' > @ c . Titel < / a >
- }
- }
代碼中,「>>」的顯示必須用Html的Raw方法顯示,否則會提示錯誤,使用「>」也不行,估計是和Razor的語法有衝突。是否有其它辦法,沒去研究了。