• MVC Music Store 在线音乐商店示例分析(10)ShoppingCartController


    ShoppingCartController购物车控制器,专门管理购物车。

    注释后源代码如下:

    1 /// <summary>
    2 /// 购物车控制器
    3 /// </summary>
    4 public class ShoppingCartController : Controller
    5 {
    6 /// <summary>
    7 /// 音乐商店实体
    8 /// </summary>
    9 MusicStoreEntities storeDB = new MusicStoreEntities();
    10
    11 //
    12 // GET: /ShoppingCart/
    13
    14 public ActionResult Index()
    15 {
    16 //获取购物车
    17 var cart = ShoppingCart.GetCart(this.HttpContext);
    18
    19 // Set up our ViewModel
    20 //设置购物车视图模型,用于装载购物项和总价
    21 var viewModel = new ShoppingCartViewModel
    22 {
    23 CartItems = cart.GetCartItems(),
    24 CartTotal = cart.GetTotal()
    25 };
    26
    27 // Return the view
    28 return View(viewModel);
    29 }
    30
    31 //
    32 // GET: /Store/AddToCart/5
    33 // 将指定项添加到购物车
    34 public ActionResult AddToCart(int id)
    35 {
    36
    37 // Retrieve the album from the database
    38 // 从数据库中检索指定编号的音乐相册
    39 var addedAlbum = storeDB.Albums
    40 .Single(album => album.AlbumId == id);
    41
    42 // Add it to the shopping cart
    43 // 获取购物车对象
    44 var cart = ShoppingCart.GetCart(this.HttpContext);
    45 //将检索到的音乐相册添加到购物车中
    46 cart.AddToCart(addedAlbum);
    47
    48 // Go back to the main store page for more shopping
    49 // 重定向到首页,以便用户添加更多的商品。
    50 return RedirectToAction("Index");
    51 }
    52
    53 //
    54 // AJAX: /ShoppingCart/RemoveFromCart/5
    55 /// <summary>
    56 /// 从购物车中移除指定编号的音乐相册
    57 /// </summary>
    58 /// <param name="id">音乐相册编号</param>
    59 /// <returns></returns>
    60 [HttpPost]
    61 public ActionResult RemoveFromCart(int id)
    62 {
    63 // Remove the item from the cart
    64 //获取购物车对象
    65 var cart = ShoppingCart.GetCart(this.HttpContext);
    66
    67 // Get the name of the album to display confirmation
    68 // 获取该编号的音乐相册的名称,以便显示确认信息
    69 string albumName = storeDB.Carts
    70 .Single(item => item.RecordId == id).Album.Title;
    71
    72 // Remove from cart
    73 //从购物车中移除该项
    74 int itemCount = cart.RemoveFromCart(id);
    75
    76 // Display the confirmation message
    77 // 构建购物车移除项模板,装载对应的消息
    78 var results = new ShoppingCartRemoveViewModel
    79 {
    80 Message = Server.HtmlEncode(albumName) +
    81 " has been removed from your shopping cart.",
    82 CartTotal = cart.GetTotal(),
    83 CartCount = cart.GetCount(),
    84 ItemCount = itemCount,
    85 DeleteId = id
    86 };
    87 //以json形式反馈移除项后的信息。
    88 return Json(results);
    89 }
    90
    91 //
    92 // GET: /ShoppingCart/CartSummary
    93 /// <summary>
    94 /// 购物车说明描述
    95 /// ChildActionOnly特性用于指示操作方法只应作为子操作进行调用。
    96 /// 子操作方法呈现部分视图的内联 HTML 标记而不是呈现整个视图。
    97 /// 标记有 ChildActionOnlyAttribute 的任何方法只能通过 Action 或 RenderAction HTML 扩展方法调用。
    98 /// http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-CN&k=k(SYSTEM.WEB.MVC.CHILDACTIONONLYATTRIBUTE);k(CHILDACTIONONLY)&rd=true
    99 /// </summary>
    100 /// <returns></returns>
    101 [ChildActionOnly]
    102 public ActionResult CartSummary()
    103 {
    104 //获取购物车对象
    105 var cart = ShoppingCart.GetCart(this.HttpContext);
    106 //在视图数据模型中填充购物车商品数量
    107 ViewData["CartCount"] = cart.GetCount();
    108 //返回部分视图
    109 return PartialView("CartSummary");
    110 }
    111 }

        

        Index Action用于展示购物车中内容。

        AddToCart Action用于将指定项添加到购物车中。

        RemoveFromCart Action用于将指定的项从购物车中移除。移除后呈现对应的消息。

        CartSummary Action 用于呈现购物车的描述信息,该Action属于部分视图,ChildActionOnly特性标记用于表示当前Action作为子操作进行调用,呈现出一个页面的一部分,用于将来组合一个完整的视图。

  • 相关阅读:
    hdu 1535 Invitation Cards(spfa)
    hdu 1317 XYZZY
    hdu 1245 Saving James Bond
    hdu 1546 Idiomatic Phrases Game
    hdu 1217 Arbitrage(佛洛依德)
    hdu 1599 find the mincost route(无向图的最小环)
    poj1579
    poj1905
    poj1384
    poj3624
  • 原文地址:https://www.cnblogs.com/stevenhqq/p/1971603.html
Copyright © 2020-2023  润新知