From b51f54e98a41d970cfbe03ee2842215c33b86541 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Thu, 10 Apr 2014 13:46:58 -0700 Subject: [PATCH] Adding a view component for cart summary 1. Added a View component named CartSummary 2. Moved the CartSummary.cshtml as default.cshtml to shared\Components --- .../Components/CartSummaryComponent.cs | 28 +++++++++++++++++++ .../Components/CartSummary/Default.cshtml} | 0 src/MusicStore/Views/Shared/_Layout.cshtml | 3 +- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/MusicStore/Components/CartSummaryComponent.cs rename src/MusicStore/Views/{ShoppingCart/CartSummary.cshtml => Shared/Components/CartSummary/Default.cshtml} (100%) diff --git a/src/MusicStore/Components/CartSummaryComponent.cs b/src/MusicStore/Components/CartSummaryComponent.cs new file mode 100644 index 0000000000..c28d5c6ea7 --- /dev/null +++ b/src/MusicStore/Components/CartSummaryComponent.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; +using MusicStore.Models; + +namespace MusicStore.Components +{ + [ViewComponent(Name = "CartSummary")] + public class CartSummaryComponent : ViewComponent + { + private MusicStoreContext db = new MusicStoreContext(); + public async Task InvokeAsync() + { + var cart = ShoppingCart.GetCart(db, this.Context); + + var cartItems = cart.GetCartItems() + .Select(a => a.Album.Title) + .OrderBy(x => x); + + //Bug: Uncommenting results in compilation error. + //ViewBag.CartCount = cartItems.Count(); + //ViewBag.CartSummary = string.Join("\n", cartItems.Distinct()); + + return View(); + } + } +} \ No newline at end of file diff --git a/src/MusicStore/Views/ShoppingCart/CartSummary.cshtml b/src/MusicStore/Views/Shared/Components/CartSummary/Default.cshtml similarity index 100% rename from src/MusicStore/Views/ShoppingCart/CartSummary.cshtml rename to src/MusicStore/Views/Shared/Components/CartSummary/Default.cshtml diff --git a/src/MusicStore/Views/Shared/_Layout.cshtml b/src/MusicStore/Views/Shared/_Layout.cshtml index 46753b0527..33d69098fa 100644 --- a/src/MusicStore/Views/Shared/_Layout.cshtml +++ b/src/MusicStore/Views/Shared/_Layout.cshtml @@ -27,8 +27,7 @@ @await Html.PartialAsync("_LoginPartial")