Asp.net MVC 3实例学习之ExtShop(三)——完成首页

本文介绍如何构建一个包含最新商品和优惠信息的首页。通过使用jQuery插件实现商品评价展示,并通过Partial视图来显示商品和新闻。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首页主要包括两部分,主体部分显示15个最新的的商品,右边则显示10条最新的优惠信息。主体部分可以直接使用传入的数据生成,也可以通过分类列表的方法生成,看个人喜好。而优惠信息则使用分类的方法生成。

在母版页_Layout.cshtml的head中增加以下文件的引用:


 
  1. < link href = " @Url.Content( " ~ / Content / jquery . ui . stars . css " ) " rel = " stylesheet " type = " text/css " / >   
  2. < script src = " @Url.Content( " ~ / Scripts / jquery - ui . min . js " ) " type = " text/javascript " > < / script >   
  3. < script src = " @Url.Content( " ~ / Scripts / jquery . ui . stars . min . js " ) " type = " text/javascript " > < / script >  

现在要完成首页的整体布局,打开Index.cshtml,讲ViewBag.Title修改为“首页——Ext商店”,然后加入以下代码:


 
  1. < div id = " contentMain " >   
  2. < span class = " header " > 最新产品 < / span >   
  3. @ { Html . RenderAction ( " Homepage " , " Product " ) ; }   
  4. < / div >   
  5. < div id = " contentRight " >   
  6. < span class = " header " > 优惠信息 < / span >   
  7. @ { Html . RenderAction ( " RightList " , " News " ) ; }   
  8. < / div >   

代码中,最新产品和优惠信息的数据都将从partial视图获取。下面在Site.css中加入以下css:


 
  1. #contentMain { float : left ; width : 580px ; display : block ; border-left : 1px solid #d3d3d3 ; border-right : 1px solid #d3d3d3 ; border-bottom : 1px solid #d3d3d3 ; }   
  2. #contentMain .header { width : 570px ; height : 28px ; background : url(/images/bk.gif) repeat-x ; line-height : 28px ; display : block ; color : #000 ; font-size : 14px ; margin : 0px ; padding-left : 10px ; }   
  3. #contentMain ul { float : left ; width : 180px ; display : block ; padding : 0 0 10px 10px ; }   
  4. #contentMain li { list-style-type : none ; }   
  5. #contentMain .title { height : 56px ; line-height : 14px ; width : 170px ; display : block ; font-size : 12px ; }   
  6. #contentMain .rating -title { float : left ; }   
  7. #contentMain .rating { float : left ; width : 90px ; }   
  8. #contentMain img { border : 0px ; width : 170px ; height : 190px ; }   
  9. #contentMain a { text-decoration : none ; color : #000 ; }   
  10. #contentMain a:hover { text-decoration : underline ; }   
  11. #contentMain a:visited { text-decoration : none ; color : #000 ; }   
  12. #contentRight { float : right ; width : 200px ; display : block ; border : 1px solid #d3d3d3 ; padding : 1px ; }   
  13. #contentRight .header { background : url(/images/leftHeader.jpg) repeat-x ; height : 28px ; line-height : 28px ; width : 190px ; display : block ; color : #fff ; font-size : 14px ; margin : 0px ; }   
  14. #contentRight span { width : 180px ; display : block ; height : 20px ; line-height : 20px ; background : transparent url(/images/point02.jpg) no-repeat scroll 0 10px ; padding-left : 10px ; margin-left : 5px ; }   
  15. #contentRight a { color : #000 ; }   
  16. #contentRight a:hover { text-decoration : underline ; }   
  17. #contentRight a:visited { color : #000 ; }   

现在要完成最新产品的显示。在Controllers文件夹添加一个“ProductController”的控制器,首先加入对“Extshop.Models”的引用,然后添加以下代码:


 
  1. [ ChildActionOnly ]
  2. public ActionResult Homepage ( )
  3. {
  4. var q = dc . T_Products . OrderByDescending ( m = > m . CreateTime ) . Take ( 15 ) ;
  5. return PartialView ( q ) ;
  6. }

代码只是很简单的从数据库取出15条记录,然后返回Partial视图。在“Homepage”上单击鼠标右键,然后创建一个Partial视图,在视图中添加以下代码:


 
  1. @ model IEnumerable < Extshop . Models . T_Products >   
  2. @ {   
  3. foreach ( var c in Model )   
  4. {   
  5. < ul >   
  6. < li > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > < img src = ' / Images / products / @ c . SamllImageUrl ' alt = " @c.Title " / > < / a > < / li >   
  7. < li class = ' title ' > < a href = ' @ Url . Action ( " Details " , " Product " , new { id = c . ProductID } ) ' > @ c . Title < / a > < / li >   
  8. < li > 市场价: < del > @ c . MarketPrice . ToString ( " C " ) < / del > < / li >   
  9. < li > 当前价: @ c . UnitPrice . ToString ( " C " ) < / li >   
  10. < li > < span class = " rating-title " > 评 价: < / span >   
  11. < div class = ' rating ' id = " @c.ProductID.ToString( " rat - 0 " ) " >   
  12. < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 1 " @ ( c . TotalRating = = 1 ? " checked='checked' " : " " ) / >   
  13. < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 2 " @ ( c . TotalRating = = 2 ? " checked='checked' " : " " ) / >   
  14. < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 3 " @ ( c . TotalRating = = 3 ? " checked='checked' " : " " ) / >   
  15. < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 4 " @ ( c . TotalRating = = 4 ? " checked='checked' " : " " ) / >   
  16. < input name = " @c.ProductID.ToString( " Star0 " ) " type = " radio " class = " star " disabled = " disabled " value = " 5 " @ ( c . TotalRating = = 5 ? " checked='checked' " : " " ) / >   
  17. < / div >   
  18. < / li >   
  19. < / ul >   
  20. }   
  21. }   

代码中12到16行的作用是生成评价图,在这里需要给每一组input不同的“name”属性以区别分组,因为产品编号是唯一的,所以使用它作为名称的一部分是最好的。

要使评价图显示,还需要在首页中添加以下代码:


 
  1. < script type = " text/javascript " >   
  2. jQuery ( function ( ) {   
  3. $ ( " div .rating " ) . stars ( ) ;   
  4. } ) ;   
  5. < / script >   

该段代码的作用是在页面显示加载完毕后,通过div元素的class属性获取所有生成评价图的对象,对它们进行初始化。

最后需要完成的是右边优惠信息的显示。在Controllers文件夹下添加一个“NewsController”控制器并添加需要的引用,然后添加以下代码:


 
  1. [ ChildActionOnly ]   
  2. public ActionResult RightList ( )   
  3. {   
  4. var q = dc . T_News . OrderByDescending ( m = > m . CreateTime ) . Take ( 10 ) ;   
  5. return PartialView ( q ) ;   
  6. }   

代码也很简单,获取10条最新优惠信息后返回Partial视图。在“RightList”上单击鼠标右键,然后创建一个Partial视图,在视图中添加以下代码:


 
  1. @ model IEnumerable < Extshop . Models . T_News >   
  2.  
  3. @ {   
  4. foreach ( var c in Model )   
  5. {   
  6. < span > < a href = ' @ Url . Action ( " Details " , " News " , new { id = c . NewsID } ) ' > @ c . Title < / a > < / span >   
  7. }   
  8. }  

代码也是相当的简单,逐条显示优惠信息的标题就行了。

这样,首页的功能就已经完成了,在浏览器打开,将看到如图1所示的结果。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值