在完成产品列表页前要做一些准备功夫。首先是去下载MvcPager用了为产品列表分页。下载的可能是基于MVC 2的,没关系,可以用在MVC 3上。如果有担心,下载源代码重新编译一次好了。下载后将DLL添加到引用里。

接着是要修改一下路由以实现“Catalog/List/[id]/[page]”的访问。打开“Global.asax.cs”文件,然后在默认路由之前添加以下代码:

                        routes . MapRoute (
                                " Catalog " ,   //   Route   name
                                " Catalog/List/{id}/{page} " ,   //   URL   with   parameters
                                new   {   controller   =   " Catalog " ,   action   =   " List " ,  page   =   1   }   //   Parameter   defaults
                        ) ;
 

现在打开CatalogController.cs文件,在文件头添加对MvcPager的引用,代码如下:

using   Webdiyer . WebControls . Mvc;

然后修改Index操作的代码如下:

                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”上单击鼠标右键,选择“添加视图”( ,今天换了中文版,菜单也变了)。在视图中添加以下代码:

@ using   Webdiyer . WebControls . Mvc ;
@ model   PagedList < Extshop . Models . T_Products >
 
@ {
        ViewBag . Title   =   " 产品列表 " ;
        PageData [ " id " ]   =   " " ;
}
        < div   class = " nav " >
                < a   href = " @Url.Action( " " ,   " Catalog " ) " > 产品 < / a >
10          < / div > < br   / >
11          < div   id = " contentMain "   style = " width:760px; " >
12                  < span   class = " header "     style = " width:750px; " > 产品列表 < / span >
13                          @ {
14                                  foreach ( var   c   in   Model )
15                                  {
16                                          < ul >
17                                          < li > < a   href = ' @ Url . Action ( " Details " ,  " Product " ,   new   {   id   =   c . ProductID   } ) ' > < img   src = ' / Images / products/ @ c . SamllImageUrl '   alt = " @c.Title "   / > < / a > < / li >
18                                          < li   class = ' title ' > < a   href = ' @ Url .Action ( " Details " ,   " Product " ,   new   {   id   =   c . ProductID   } ) ' > @ c . Title< / a > < / li >
19                                          < li > 市场价: < del > @ c . MarketPrice . ToString ( "C " ) < / del > < / li >
20                                          < li > 当前价: @ c . UnitPrice . ToString ( " C " ) < /li >
21                                          < li > < span   class = " rating-title " > 评 价: < /span >
22                                                  < div   class = ' rating '   id = "@c.ProductID.ToString( " rat - " ) " >                                        
23                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 1 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
24                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 2 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )   / >
25                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 3 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
26                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 4 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
27                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 5 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >                                        
28                                                  < / div >
29                                          < / li >
30                                          < / ul >                
31                                  }
32                          }
33                  < div   class = " clear " > < / div >
34                  < div   class = ' pagenav ' > @ Html . Pager ( Model ,   new   PagerOptions   {  PageIndexParameterName   =   " id "   } ) < / div >                        
35          < / div >
36                 
37          < script   type = " text/javascript " >
38                  jQuery ( function   ( )   {
39                          $ ( " div   .rating " ) . stars ( ) ;
40                  } ) ;
41          < / script >
42   
 
 

代码第一句引用MvcPager,第二句定义接收的数据模型类型。因为是所以产品列表,所以PageData["id"]设置为空字符串。其余代码和首页的差不多。第34句使用HTML方式显示分页代码。这里要注意的是PagerOptions选项中的PageIndexParameterName要设置为id,因为操作中接收的当前页参数是以id定义的。

页面需要正常显示,还需要在Site.css中添加以下代码:

#contentMain   .clear { clear : both ; }
#contentMain   .pagenav { width : 760px ; text-align : center ; margin-bottom : 10px ; }

切换回CatalogController.cs文件,在Index操作下添加一个List操作,其代码如下:

                public   ActionResult   List(string   id,   int?   page)
                {
                        ViewData["id"]   =   id;
                        PagedList   q   =   dc.T_Products
                                .Where(m   =>   dc.T_Categories.Where(n   =>  n.CategoryID.Substring(0,   id.Length)   ==   id).Select(n   =>     n.CategoryID  ).Contains(m.CategoryID))
                                .OrderByDescending(m   =>   m.CreateTime).ToPagedList(page   ??  1,   4);
                        return   View(q);
                }
 

从代码可以看到,List操作有两个参数,第1个是分类id,第2个是当前页号。第3行通过ViewData["id"]向视图传递当前的分类id,以更新左边分类列表以及显示导航条。其余的和Index操作差不多。现在为List操作创建一个视图,视图内代码如下:

@ 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 >
10                  @ { Html . RenderAction ( " Navbar " ,   " Catalog " ,   new   {   id   =  ViewData [ " id " ]   } ) ; }
11          < / div > < br   / >
12          < div   id = " contentMain "   style = " width:760px; " >
13                  < span   class = " header "   style = " width:750px; " > 产品列表 < / span >
14                          @ {
15                                  foreach ( var   c   in   Model )
16                                  {
17                                          < ul >
18                                          < li > < a   href = ' @ Url . Action ( " Details " ,  " Product " ,   new   {   id   =   c . ProductID   } ) ' > < img   src = ' / Images / products/ @ c . SamllImageUrl '   alt = " @c.Title "   / > < / a > < / li >
19                                          < li   class = ' title ' > < a   href = ' @ Url .Action ( " Details " ,   " Product " ,   new   {   id   =   c . ProductID   } ) ' > @ c .Title < / a > < / li >
20                                          < li > 市场价: < del > @ c . MarketPrice . ToString (" C " ) < / del > < / li >
21                                          < li > 当前价: @ c . UnitPrice . ToString ( " C " ) </ li >
22                                          < li > < span   class = " rating-title " > 评 价: < /span >
23                                                  < div   class = ' rating '   id = "@c.ProductID.ToString( " rat - " ) " >                                        
24                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 1 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
25                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 2 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )   / >
26                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 3 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
27                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 4 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >
28                                                          < input   name = "@c.ProductID.ToString( " Star0 " ) "   type = " radio "   class = " star "   disabled = "disabled "   value = " 5 "   @ ( c . TotalRating   = =     ?   " checked='checked' "   :   "" )     / >                                        
29                                                  < / div >
30                                          < / li >
31                                          < / ul >                
32                                  }
33                          }
34                  < div   class = " clear " > < / div >
35                  < div   class = ' pagenav ' > @ Html . Pager ( Model ,   new   PagerOptions  {   PageIndexParameterName   =   " page "   } ) < / div >                        
36          < / div >
37                 
38          < script   type = " text/javascript " >
39                  jQuery ( function   ( )   {
40                          $ ( " div   .rating " ) . stars ( ) ;
41                  } ) ;
42          < / script >
43   
44   
45   

代码第10行通过一个分部视图显示导航菜单。第35行要注意PageIndexParameterName是“page”,而不是Index视图的“id”了。

下一步要做的是完成导航条的显示。将文件切换回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 ) ) ;
                        }
10                          var   q   =   dc . T_Categories . Where ( m   = >   idlist . Contains ( m. CategoryID ) ) . OrderBy ( m   = >   m . CategoryID ) ;
11                          return   PartialView ( q ) ;
12                  }
13   

代码首先获取了当前分类的父类编号,然后从数据库一次查询出所有类别并排序。现在为该子操作创建一个分部视图,视图代码如下:

@ 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的语法有冲突。是否有其它办法,没去研究了。

至此,产品列表页的就已经完成了。

BTW:因为项目还没完成,所以没考虑提供源代码,但有读者希望在看文章的同时能提供源代码做参考,所以从本文开始,每完成一节,就提供到该节的代码。

源代码:http://download.csdn.net/source/2996287

Asp.net MVC 3实例学习之ExtShop(四)——完成产品列表页的更多相关文章

  1. ASP.NET MVC 5 学习教程:修改视图和布局页

    原文 ASP.NET MVC 5 学习教程:修改视图和布局页 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...

  2. ASP.NET MVC 网站开发总结(四)——校友平台开发总结

    又历经一个多月的努力,学校的一个校友平台项目也接近内测的尾声了,简单的总结一下这次的项目开发. 与上次做Wing工作室的门户网站相比,同样是团队开发,参与的人员多了一个,用的时间也差不多一个月,但从总 ...

  3. ASP.NET MVC下使用AngularJs语言(四):$window.alert

    判断文本框是否有填写,没有填写使用angularjs的$window.alert来提示用户. 创建一个ASP.NET MVC控制器: 接下来是准备一个angularjs的控制器: pilotApp.c ...

  4. ASP.NET MVC之下拉框绑定四种方式(十)

    前言 上两节我们讲了文件上传的问题,关于这个上传的问题还未结束,我也在花时间做做分割大文件处理以及显示进度的问题,到时完成的话再发表,为了不耽误学习MVC其他内容的计划,我们今天开始好好讲讲关于MVC ...

  5. ASP.NET MVC 设置Area中 Controller 的方法 默认启动页

    MVC中通常分区域编程,互不干扰,如果需要设置某个区域下面的某个控制器下面的某个方法为默认启动页的话,直接修改项目的路由如下: public static void RegisterRoutes(Ro ...

  6. 《React后台管理系统实战 :四》产品分类管理页:添加产品分类、修改(更新)产品分类

    一.静态页面 目录结构 F:\Test\react-demo\admin-client\src\pages\admin\category add-cate-form.jsx index.jsx ind ...

  7. ASP.Net请求处理机制初步探索之旅 - Part 5 ASP.Net MVC请求处理流程

    好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人>: --> 开篇:上一篇 ...

  8. ASP.NET MVC 4.0的Action Filter

    有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你自定义创建action过滤器.Action过滤器是自定义的Attributes,用来 ...

  9. ASP.Net MVC请求处理流程

    ASP.Net MVC请求处理流程 好听的歌 我一直觉得看一篇文章再听一首好听的歌,真是种享受.于是,我在这里嵌入一首好听的歌,当然你觉得不想听的话可以点击停止,歌曲 from 王菲 <梦中人& ...

随机推荐

  1. Net数值计算MathNet.Numerics类库

    一.Net自带的数值计算:System.Numerics 1.大整数BitInteger 方法:除数和余数.最大公约数 2.复数Complex 属性:实部.虚部.量值.相位 方法:共轭.倒数 二.Ma ...

  2. CSS中常用的字体单位:px、em、rem和%的区别

    在刚接触CSS时,px用的比较多,也很好理解,可是用久了就会发现有些缺陷,特别是在做响应式开发的时候. 那这么多单位到底在什么时候用什么单位合适呢?今天就来探讨一下. 先大致解释一下这些单位的意思: ...

  3. datepicker,结束时间必须大于开始时间

    $('#js-start-time').datepicker({ dateFormat:'yy-mm-dd', onSelect: function( startDate ) { var $start ...

  4. 如何实现Windows Phone代码与Unity相互通信(插件方式)

    原地址:http://www.cnblogs.com/petto/p/3915943.html 一些废话 原文地址: http://imwper.com/unity/petto/%E5%A6%82%E ...

  5. DirectShow 最简单的入门 -- 播放一段视频

    #include <dshow.h> #pragma comment(lib,"strmbase.lib") #pragma comment(lib,"qua ...

  6. 有趣 GIF 动图集 - 仿佛每张小动图都诉说了一个小笑话或者小故事

    点这里 来自法国南特(Nantes)的 Guillaume Kurkdjian 目前还是个学生.Kurkdjian 擅长创作一些平面动态图像,这些有趣的小动图仿佛每张都诉说了一个小笑话或者小故事,像个 ...

  7. 关于com组件注册的问题

    问题是这样的: 在调用摄像头的时候,用到com组件,我已经在工程中添加了com组件,但是运行的时候却报这样的错误. 解决方案:程序生成中,目标平台为Any CPU ,应该改为x86 具体原因不知道……

  8. Mime Types

    Mime Types 1.http://www.freeformatter.com/mime-types-list.html 2.http://www.webmaster-toolkit.com/mi ...

  9. javascript中onclick事件能调用多个方法吗

    Q: javascript中onclick事件能调用多个方法吗? A: 可以的,方法如下onclick="aa();bb();cc();"每个方法用“;”分号隔开就行了

  10. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...