5.
OverviewThe MVC Music Store is a tutorial application that introduces and explains step-by-step how to use ASP.NETMVC and Visual Web Developer for web development. We’ll be starting slowly, so beginner level webdevelopment experience is okay.The application we’ll be building is a simple music store. There are three main parts to the application: shopping,checkout, and administration. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 4

6.
Visitors can browse Albums by Genre:They can view a single album and add it to their cart: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 5

7.
They can review their cart, removing any items they no longer want:Proceeding to Checkout will prompt them to login or register for a user account. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 6

10.
In addition to customer-faceing pages, we’ll also build an administrator section that shows a list of albums fromwhich Administrators can Create, Edit, and Delete albums:1. File -> New ProjectInstalling the softwareThis tutorial will begin by creating a new ASP.NET MVC 3 project using the free Visual Web Developer 2010Express (which is free), and then we’ll incrementally add features to create a complete functioning application.Along the way, we’ll cover database access, form posting scenarios, data validation, using master pages forconsistent page layout, using AJAX for page updates and validation, user login, and more.You can follow along step by step, or you can download the completed application fromhttp://mvcmusicstore.codeplex.com.You can use either Visual Studio 2010 SP1 or Visual Web Developer 2010 Express SP1 (a free version of VisualStudio 2010) to build the application. We’ll be using the SQL Server Compact (also free) to host the database.Before you start, make sure youve installed the prerequisites listed below. You can install all of them using thefollowing Web Platform Installerlink: http://www.microsoft.com/web/gallery/install.aspx?appid=VWD2010SP1PackNote: You can find this link on the big green button at this (easier to remember) link: http://asp.net/mvc MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 9

11.
The Web Platform Installer will check what you’ve got installed and just download what you need.If you want to individually install the prerequisites using the following links instead of using the above link, usethe following links (written out in case you’re using a printed version of this tutorial): MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 10

14.
This will create our project. Let’s take a look at the folders that have been added to our application in theSolution Explorer on the right side. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 13

16.
/Models Models hold and manipulate data/Content This folder holds our images, CSS, and any other static content/Scripts This folder holds our JavaScript filesThese folders are included even in an Empty ASP.NET MVC application because the ASP.NET MVC framework bydefault uses a “convention over configuration” approach and makes some default assumptions based on foldernaming conventions. For instance, controllers look for views in the Views folder by default without you havingto explicitly specify this in your code. Sticking with the default conventions reduces the amount of code youneed to write, and can also make it easier for other developers to understand your project. We’ll explain theseconventions more as we build our application.2. ControllersWith traditional web frameworks, incoming URLs are typically mapped to files on disk. For example: a requestfor a URL like "/Products.aspx" or "/Products.php" might be processed by a "Products.aspx" or "Products.php"file.Web-based MVC frameworks map URLs to server code in a slightly different way. Instead of mapping incomingURLs to files, they instead map URLs to methods on classes. These classes are called "Controllers" and they areresponsible for processing incoming HTTP requests, handling user input, retrieving and saving data, anddetermining the response to send back to the client (display HTML, download a file, redirect to a different URL,etc.).Adding a HomeControllerWe’ll begin our MVC Music Store application by adding a Controller class that will handle URLs to the Homepage of our site. We’ll follow the default naming conventions of ASP.NET MVC and call it HomeController.Right-click the “Controllers” folder within the Solution Explorer and select “Add”, and then the “Controller…”command: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 15

18.
This will create a new file, HomeController.cs, with the following code:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcMusicStore.Controllers{ public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } }}To start as simply as possible, let’s replace the Index method with a simple method that just returns a string.We’ll make two changes:  Change the method to return a string instead of an ActionResult  Change the return statement to return “Hello from Home”The method should now look like this:public string Index(){ return "Hello from Home";}Running the ApplicationNow let’s run the site. We can start our web-server and try out the site using any of the following::  Choose the Debug ⇨Start Debugging menu item  Click the Green arrow button in the toolbar  Use the keyboard shortcut, F5. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 17

19.
Using any of the above steps will compile our project, and then cause the ASP.NET Development Server that isbuilt-into Visual Web Developer to start. A notification will appear in the bottom corner of the screen to indicatethat the ASP.NET Development Server has started up, and will show the port number that it is running under.Visual Web Developer will then automatically open a browser window whose URL points to our web-server.This will allow us to quickly try out our web application:Okay, that was pretty quick – we created a new website, added a three line function, and we’ve got text in abrowser. Not rocket science, but it’s a start.Note: Visual Web Developer includes the ASP.NET Development Server, which will run your website on a randomfree “port” number. In the screenshot above, the site is running at http://localhost:26641/, so it’s using port26641. Your port number will be different. When we talk about URL’s like /Store/Browse in this tutorial, that willgo after the port number. Assuming a port number of 26641, browsing to /Store/Browse will mean browsing tohttp://localhost:26641/Store/Browse. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 18

20.
Adding a StoreControllerWe added a simple HomeController that implements the Home Page of our site. Let’s now add anothercontroller that we’ll use to implement the browsing functionality of our music store. Our store controller willsupport three scenarios:  A listing page of the music genres in our music store  A browse page that lists all of the music albums in a particular genre  A details page that shows information about a specific music albumWe’ll start by adding a new StoreController class.. If you haven’t already, stop running the application either byclosing the browser or selecting the Debug ⇨Stop Debugging menu item.Now add a new StoreController. Just like we did with HomeController, we’ll do this by right-clicking on the“Controllers” folder within the Solution Explorer and choosing the Add->Controller menu itemOur new StoreController already has an “Index” method. We’ll use this “Index” method to implement ourlisting page that lists all genres in our music store. We’ll also add two additional methods to implement the twoother scenarios we want our StoreController to handle: Browse and Details.These methods (Index, Browse and Details) within our Controller are called “Controller Actions”, and as you’vealready seen with the HomeController.Index()action method, their job is to respond to URL requests and(generally speaking) determine what content should be sent back to the browser or user that invoked the URL. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 19

22.
That’s great, but these are just constant strings. Let’s make them dynamic, so they take information from theURL and display it in the page output.First we’ll change the Browse action method to retrieve a querystring value from the URL. We can do this byadding a “genre” parameter to our action method. When we do this ASP.NET MVC will automatically pass anyquerystring or form post parameters named “genre” to our action method when it is invoked.//// GET: /Store/Browse?genre=Discopublic string Browse(string genre){ string message = HttpUtility.HtmlEncode("Store.Browse, Genre = " + genre); return message;}Note: We’re using the HttpUtility.HtmlEncode utility method to sanitize the user input. This prevents users frominjecting Javascript into our View with a link like/Store/Browse?Genre=<script>window.location=’http://hackersite.com’</script>.Now let’s browse to /Store/Browse?Genre=Disco MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 21

23.
Let’s next change the Details action to read and display an input parameter named ID. Unlike our previousmethod, we won’t be embedding the ID value as a querystring parameter. Instead we’ll embed it directly withinthe URL itself. For example: /Store/Details/5.ASP.NET MVC lets us easily do this without having to configure anything. ASP.NET MVC’s default routingconvention is to treat the segment of a URL after the action method name as a parameter named “ID”. If youraction method has a parameter named ID then ASP.NET MVC will automatically pass the URL segment to you asa parameter.//// GET: /Store/Details/5public string Details(int id){ string message = "Store.Details, ID = " + id; return message;}Run the application and browse to /Store/Details/5: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 22

25.
3. Views and ModelsSo far we’ve just been returning strings from controller actions. That’s a nice way to get an idea of howcontrollers work, but it’s not how you’d want to build a real web application. We are going to want a better wayto generate HTML back to browsers visiting our site – one where we can use template files to more easilycustomize the HTML content send back. That’s exactly what Views do.Adding a View templateTo use a view-template, we’ll change the HomeController Index method to return an ActionResult, and have itreturn View(), like below:public class HomeController : Controller{ // // GET: /Home/ public ActionResult Index() { return View(); }}The above change indicates that instead of returned a string, we instead want to use a “View” to generate aresult back.We’ll now add an appropriate View template to our project. To do this we’ll position the text cursor within theIndex action method, then right-click and select “Add View”. This will bring up the Add View dialog: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 24

26.
The “Add View” dialog allows us to quickly and easily generate View template files. By default the “Add View”dialog pre-populates the name of the View template to create so that it matches the action method that will useit. Because we used the “Add View” context menu within the Index() action method of our HomeController, the“Add View” dialog above has “Index” as the view name pre-populated by default. We don’t need to change anyof the options on this dialog, so click the Add button.When we click the Add button, Visual Web Developer will create a new Index.cshtml view template for us in theViewsHome directory, creating the folder if doesn’t already exist. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 25

27.
The name and folder location of the “Index.cshtml” file is important, and follows the default ASP.NET MVCnaming conventions. The directory name, ViewsHome, matches the controller - which is namedHomeController. The view template name, Index, matches the controller action method which will be displayingthe view.ASP.NET MVC allows us to avoid having to explicitly specify the name or location of a view template when weuse this naming convention to return a view. It will by default render the ViewsHomeIndex.cshtml viewtemplate when we write code like below within our HomeController:public class HomeController : Controller{ // // GET: /Home/ public ActionResult Index() { return View(); }}Visual Web Developer created and opened the “Index.cshtml” view template after we clicked the “Add” buttonwithin the “Add View” dialog. The contents of Index.cshtml are shown below.@{ MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 26

28.
ViewBag.Title = "Index";}<h2>Index</h2>This view is using the Razor syntax, which is more concise than the Web Forms view engine used in ASP.NETWeb Forms and previous versions of ASP.NET MVC. The Web Forms view engine is still available in ASP.NET MVC3, but many developers find that the Razor view engine fits ASP.NET MVC development really well.The first three lines set the page title using ViewBag.Title. We’ll look at how this works in more detail soon, butfirst let’s update the text heading text and view the page. Update the <h2> tag to say “This is the Home Page” asshown below.@{ ViewBag.Title = "Index";}<h2>This is the Home Page</h2>Running the application shows that our new text is visible on the home page.Using a Layout for common site elementsMost websites have content which is shared between many pages: navigation, footers, logo images, stylesheetreferences, etc. The Razor view engine makes this easy to manage using a page called _Layout.cshtml which hasautomatically been created for us inside the /Views/Shared folder. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 27

29.
Double-click on this folder to view the contents, which are shown below.<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script></head><body> @RenderBody()</body></html>The content from our individual views will be displayed by the @RenderBody() command, and any commoncontent that we want to appear outside of that can be added to the _Layout.cshtml markup. We’ll want ourMVC Music Store to have a common header with links to our Home page and Store area on all pages in the site,so we’ll add that to the template directly above that @RenderBody() statement.<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>@ViewBag.Title</title> <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 28

30.
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script></head><body> <div id="header"> <h1> ASP.NET MVC MUSIC STORE</h1> <ul id="navlist"> <li class="first"><a href="/" id="current">Home</a></li> <li><a href="/Store/">Store</a></li> </ul> </div> @RenderBody()</body></html>Updating the StyleSheetThe empty project template includes a very streamlined CSS file which just includes styles used to displayvalidation messages. Our designer has provided some additional CSS and images to define the look and feel forour site, so we’ll add those in now.The updated CSS file and Images are included in the Content directory of MvcMusicStore-Assets.zip which isavailable at http://mvcmusicstore.codeplex.com. We’ll select both of them in Windows Explorer and drop theminto our Solution’s Content folder in Visual Web Developer, as shown below:You’ll be asked to confirm if you want to overwrite the existing Site.css file. Click Yes. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 29

31.
The Content folder of your application will now appear as follows:Now lets run the application and see how our changes look on the Home page. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 30

32.
 Let’s review what’s changed: The HomeController’s Index action method found and displayed the ViewsHomeIndex.cshtmlView template, even though our code called “return View()”, because our View template followed the standard naming convention.  The Home Page is displaying a simple welcome message that is defined within the ViewsHomeIndex.cshtml view template.  The Home Page is using our _Layout.cshtml template, and so the welcome message is contained within the standard site HTML layout.Using a Model to pass information to our ViewA View template that just displays hardcoded HTML isn’t going to make a very interesting web site. To create adynamic web site, we’ll instead want to pass information from our controller actions to our view templates.In the Model-View-Controller pattern, the term Model refers to objects which represent the data in theapplication. Often, model objects correspond to tables in your database, but they don’t have to.Controller action methods which return an ActionResult can pass a model object to the view. This allows aController to cleanly package up all the information needed to generate a response, and then pass thisinformation off to a View template to use to generate the appropriate HTML response. This is easiest tounderstand by seeing it in action, so let’s get started.First we’ll create some Model classes to represent Genres and Albums within our store. Let’s start by creating aGenre class. Right-click the “Models” folder within your project, choose the “Add Class” option, and name thefile “Genre.cs”. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 31

34.
Note: In case youre wondering, the { get; set; } notation is making use of C#s auto-implemented propertiesfeature. This gives us the benefits of a property without requiring us to declare a backing field.Next, follow the same steps to create an Album class (named Album.cs) that has a Title and a Genre property:public class Album{ public string Title { get; set; } public Genre Genre { get; set; }}Now we can modify the StoreController to use Views which display dynamic information from our Model. If - fordemonstration purposes right now - we named our Albums based on the request ID, we could display thatinformation as in the view below.We’ll start by changing the Store Details action so it shows the information for a single album. Add a “using”statement to the top of the StoreControllers class to include the MvcMusicStore.Models namespace, so wedon’t need to type MvcMusicStore.Models.Album every time we want to use the album class. The “usings”section of that class should now appear as below.using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using MvcMusicStore.Models;Next, we’ll update the Details controller action so that it returns an ActionResult rather than a string, as we didwith the HomeController’s Index method. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 33

35.
public ActionResult Details(int id)Now we can modify the logic to return an Album object to the view. Later in this tutorial we will be retrievingthe data from a database – but for right now we will use "dummy data" to get started.public ActionResult Details(int id){ var album = new Album { Title = "Album " + id }; return View(album);}Note: If you’re unfamiliar with C#, you may assume that using var means that our album variable is late-bound.That’s not correct – the C# compiler is using type-inference based on what we’re assigning to the variable todetermine that album is of type Album and compiling the local album variable as an Album type, so we getcompile-time checking and Visual Studio code-editor support.Let’s now create a View template that uses our Album to generate an HTML response. Before we do that weneed to build the project so that the Add View dialog knows about our newly created Album class. You can buildthe project by selecting the Debug⇨ Build MvcMusicStore menu item (for extra credit, you can use the Ctrl-Shift-B shortcut to build the project).Now that weve set up our supporting classes, were ready to build our View template. Right-click within theDetails method and select “Add View…” from the context menu.We are going to create a new View template like we did before with the HomeController. Because we arecreating it from the StoreController it will by default be generated in a ViewsStoreIndex.cshtml file. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 34

36.
Unlike before, we are going to check the “Create a strongly-typed” view checkbox. We are then going to selectour “Album” class within the “View data-class” drop-downlist. This will cause the “Add View” dialog to create aView template that expects that an Album object will be passed to it to use.When we click the “Add” button our ViewsStoreDetails.cshtml View template will be created, containing thefollowing code.@model MvcMusicStore.Models.Album@{ ViewBag.Title = "Details";}<h2>Details</h2>Notice the first line, which indicates that this view is strongly-typed to our Album class. The Razor view engineunderstands that it has been passed an Album object, so we can easily access model properties and even havethe benefit of IntelliSense in the Visual Web Developer editor. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 35

37.
Update the <h2> tag so it displays the Album’s Title property by modifying that line to appear as follows.<h2>Album: @Model.Title</h2>Notice that IntelliSense is triggered when you enter the period after the @Model keyword, showing theproperties and methods that the Album class supports.Lets now re-run our project and visit the /Store/Details/5 URL. Well see details of an Album like below.Now we’ll make a similar update for the Store Browse action method. Update the method so it returns anActionResult, and modify the method logic so it creates a new Genre object and returns it to the View.public ActionResult Browse(string genre) MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 36

39.
Now let’s re-run our project and browse to the /Store/Browse?Genre=Disco URL. We’ll see the Browse pagedisplayed like below.Finally, let’s make a slightly more complex update to the Store Index action method and view to display a list ofall the Genres in our store. We’ll do that by using a List of Genres as our model object, rather than just a singleGenre.public ActionResult Index(){ var genres = new List<Genre> { new Genre { Name = "Disco"}, new Genre { Name = "Jazz"}, new Genre { Name = "Rock"} }; return View(genres);}Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, andpress the Add button. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 38

40.
First we’ll change the @model declaration to indicate that the view will be expecting several Genre objectsrather than just one. Change the first line of /Store/Index.cshtml to read as follows:@model IEnumerable<MvcMusicStore.Models.Genre>This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.We’re using an IEnumerable<Genre> rather than a List<Genre> since it’s more generic, allowing us to changeour model type later to any object type that supports the IEnumerable interface.Next, we’ll loop through the Genre objects in the model as shown in the completed view code below.@model IEnumerable<MvcMusicStore.Models.Genre>@{ ViewBag.Title = "Store";}<h3>Browse Genres</h3><p> Select from @Model.Count() genres:</p><ul> MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 39

41.
@foreach (var genre in Model) { <li>@genre.Name</li> }</ul>Notice that we have full IntelliSense support as we enter this code, so that when we type “@Model.” we see allmethods and properties supported by an IEnumerable of type Genre.Within our “foreach” loop, Visual Web Developer knows that each item is of type Genre, so we see IntelliSencefor each the Genre type.Next, the scaffolding feature examined the Genre object and determined that each will have a Name property,so it loops through and writes them out. It also generates Edit, Details, and Delete links to each individual item.We’ll take advantage of that later in our store manager, but for now we’d like to have a simple list instead.When we run the application and browse to /Store, we see that both the count and list of Genres is displayed. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 40

42.
Adding Links between pagesOur /Store URL that lists Genres currently lists the Genre names simply as plain text. Let’s change this so thatinstead of plain text we instead have the Genre names link to the appropriate /Store/Browse URL, so thatclicking on a music genre like “Disco” will navigate to the /Store/Browse?genre=Disco URL. We could update ourViewsStoreIndex.cshtml View template to output these links using code like below (don’t type this in - we’regoing to improve on it):<ul> @foreach (var genre in Model) { <li><a href="/Store/Browse?genre=@genre.Name">@genre.Name</a></li> }</ul>That works, but it could lead to trouble later since it relies on a hardcoded string. For instance, if we wanted torename the Controller, we’d need to search through our code looking for links that need to be updated.An alternative approach we can use is to take advantage of an HTML Helper method. ASP.NET MVC includesHTML Helper methods which are available from our View template code to perform a variety of common tasksjust like this. The Html.ActionLink() helper method is a particularly useful one, and makes it easy to build HTML<a> links and takes care of annoying details like making sure URL paths are properly URL encoded. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 41

43.
Html.ActionLink() has several different overloads to allow specifying as much information as you need for yourlinks. In the simplest case, you’ll supply just the link text and the Action method to go to when the hyperlink isclicked on the client. For example, we can link to “/Store/” Index() method on the Store Details page with thelink text “Go to the Store Index” using the following call:@Html.ActionLink("Go to the Store Index", "Index")Note: In this case, we didn’t need to specify the controller name because we’re just linking to another actionwithin the same controller that’s rendering the current view.Our links to the Browse page will need to pass a parameter, though, so we’ll use another overload of theHtml.ActionLink method that takes three parameters: 1. Link text, which will display the Genre name 2. Controller action name (Browse) 3. Route parameter values, specifying both the name (Genre) and the value (Genre name)Putting that all together, here’s how we’ll write those links to the Store Index view:<ul> @foreach (var genre in Model) { <li>@Html.ActionLink(genre.Name, "Browse", new { genre = genre.Name })</li> }</ul>Now when we run our project again and access the /Store/ URL we will see a list of genres. Each genre is ahyperlink – when clicked it will take us to our /Store/Browse?genre=[genre] URL. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 42

45.
4. Data AccessSo far, we’ve just been passing “dummy data” from our Controllers to our View templates. Now we’re ready tohook up a real database. In this tutorial we’ll be covering how to use SQL Server Compact Edition (often calledSQL CE) as our database engine. SQL CE is a free, embedded, file based database that doesn’t require anyinstallation or configuration, which makes it really convenient for local development.Database access with Entity Framework Code-FirstWe’ll use the Entity Framework (EF) support that is included in ASP.NET MVC 3 projects to query and update thedatabase. EF is a flexible object relational mapping (ORM) data API that enables developers to query and updatedata stored in a database in an object-oriented way.Entity Framework version 4 supports a development paradigm called code-first. Code-first allows you tocreate model object by writing simple classes (also known as POCO from "plain-old" CLR objects), and caneven create the database on the fly from your classes.Changes to our Model ClassesWe will be leveraging the database creation feature in Entity Framework in this tutorial. Before we do that,though, let’s make a few minor changes to our model classes to add in some things we’ll be using later on.Adding the Artist Model ClassesOur Albums will be associated with Artists, so we’ll add a simple model class to describe an Artist. Add a newclass to the Models folder named Artist.cs using the code shown below.namespace MvcMusicStore.Models{ public class Artist { public int ArtistId { get; set; } public string Name { get; set; } }}Updating our Model ClassesUpdate the Album class as shown below.namespace MvcMusicStore.Models{ public class Album { public int AlbumId { get; set; } public int GenreId { get; set; } public int ArtistId { get; set; } public string Title { get; set; } public decimal Price { get; set; } public string AlbumArtUrl { get; set; } public Genre Genre { get; set; } public Artist Artist { get; set; } MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 44

47.
Creating a Connection String in the web.config fileWe will add a few lines to the website’s configuration file so that Entity Framework knows how to connect toour database. Double-click on the Web.config file located in the root of the project.Scroll to the bottom of this file and add a <connectionStrings> section directly above the last line, as shownbelow. <connectionStrings> <add name="MusicStoreEntities" connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings></configuration>Adding a Context ClassRight-click the Models folder and add a new class named MusicStoreEntities.cs. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 46

48.
This class will represent the Entity Framework database context, and will handle our create, read, update, anddelete operations for us. The code for this class is shown below.using System.Data.Entity;namespace MvcMusicStore.Models{ public class MusicStoreEntities : DbContext { public DbSet<Album> Albums { get; set; } public DbSet<Genre> Genres { get; set; } }}That’s it - there’s no other configuration, special interfaces, etc. By extending the DbContext base class, ourMusicStoreEntities class is able to handle our database operations for us. Now that we’ve got that hooked up,let’s add a few more properties to our model classes to take advantage of some of the additional information inour database.Adding our store catalog dataWe will take advantage of a feature in Entity Framework which adds “seed” data to a newly created database.This will pre-populate our store catalog with a list of Genres, Artists, and Albums. The MvcMusicStore-Assets.zipdownload - which included our site design files used earlier in this tutorial - has a class file with this seed data,located in a folder named Code.Within the Code / Models folder, locate the SampleData.cs file and drop it into the Models folder in our project,as shown below. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 47

49.
Now we need to add one line of code to tell Entity Framework about that SampleData class. Double-click on theGlobal.asax file in the root of the project to open it and add the following line to the top the Application_Startmethod.protected void Application_Start(){ System.Data.Entity.Database.SetInitializer(new MvcMusicStore.Models.SampleData()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes);}At this point, we’ve completed the work necessary to configure Entity Framework for our project.Querying the DatabaseNow let’s update our StoreController so that instead of using “dummy data” it instead calls into our database toquery all of its information. We’ll start by declaring a field on the StoreController to hold an instance of theMusicStoreEntities class, named storeDB:public class StoreController : Controller{ MusicStoreEntities storeDB = new MusicStoreEntities(); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 48

50.
Updating the Store Index to query the databaseThe MusicStoreEntities class is maintained by the Entity Framework and exposes a collection property for eachtable in our database. Let’s update our StoreController’s Index action to retrieve all Genres in our database.Previously we did this by hard-coding string data. Now we can instead just use the Entity Framework contextGeneres collection:public ActionResult Index(){ var genres = storeDB.Genres.ToList(); return View(genres);}No changes need to happen to our View template since we’re still returning the same StoreIndexViewModel wereturned before - we’re just returning live data from our database now.When we run the project again and visit the “/Store” URL, we’ll now see a list of all Genres in our database:Updating Store Browse and Details to use live dataWith the /Store/Browse?genre=[some-genre] action method, we’re searching for a Genre by name. We onlyexpect one result, since we shouldn’t ever have two entries for the same Genre name, and so we can use the.Single() extension in LINQ to query for the appropriate Genre object like this (don’t type this yet):var example = storeDB.Genres.Single(g => g.Name == “Disco”); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 49

51.
The Single method takes a Lambda expression as a parameter, which specifies that we want a single Genreobject such that its name matches the value we’ve defined. In the case above, we are loading a single Genreobject with a Name value matching Disco.We’ll take advantage of an Entity Framework feature that allows us to indicate other related entities we wantloaded as well when the Genre object is retrieved. This feature is called Query Result Shaping, and enables us toreduce the number of times we need to access the database to retrieve all of the information we need. We wantto pre-fetch the Albums for Genre we retrieve, so we’ll update our query to include fromGenres.Include(“Albums”) to indicate that we want related albums as well. This is more efficient, since it willretrieve both our Genre and Album data in a single database request.With the explanations out of the way, here’s how our updated Browse controller action looks:public ActionResult Browse(string genre){ // Retrieve Genre and its Associated Albums from database var genreModel = storeDB.Genres.Include("Albums") .Single(g => g.Name == genre); return View(genreModel);}We can now update the Store Browse View to display the albums which are available in each Genre. Open theview template (found in /Views/Store/Browse.cshtml) and add a bulleted list of Albums as shown below.@model MvcMusicStore.Models.Genre@{ ViewBag.Title = "Browse";}<h2>Browsing Genre: @Model.Name</h2><ul> @foreach (var album in Model.Albums) { <li> @album.Title </li> }</ul>Running our application and browsing to /Store/Browse?genre=Jazz shows that our results are now being pulledfrom the database, displaying all albums in our selected Genre. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 50

53.
Now that our Store Details page is set up to display an album by the Album ID, let’s update the Browse view tolink to the Details view. We will use Html.ActionLink, exactly as we did to link from Store Index to Store Browseat the end of the previous section. The complete source for the Browse view appears below.@model MvcMusicStore.Models.Genre@{ ViewBag.Title = "Browse";}<h2>Browsing Genre: @Model.Name</h2><ul> @foreach (var album in Model.Albums) { <li> @Html.ActionLink(album.Title, "Details", new { id = album.AlbumId }) </li> }</ul>We’re now able to browse from our Store page to a Genre page, which lists the available albums, and by clickingon an album we can view details for that album. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 52

55.
5. Edit Forms using ScaffoldingIn the past chapter, we were loading data from our database and displaying it. In this chapter, we’ll also enableediting the data.Creating the StoreManagerControllerWe’ll begin by creating a new controller called StoreManagerController. For this controller, we will be takingadvantage of the Scaffolding features available in the ASP.NET MVC 3 Tools Update. Set the options for the AddController dialog as shown below.When you click the Add button, you’ll see that the ASP.NET MVC 3 scaffolding mechanism does a good amountof work for you:  It creates the new StoreManagerController with a local Entity Framework variable  It adds a StoreManager folder to the project’s Views folder  It adds Create.cshtml, Delete.cshtml, Details.cshtml, Edit.cshtml, and Index.cshtml view, strongly typed to the Album class MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 54

56.
The new StoreManager controller class includes CRUD (create, read, update, delete) controller actions whichknow how to work with the Album model class and use our Entity Framework context for database access.Modifying a Scaffolded ViewIt’s important to remember that, while this code was generated for us, it’s standard ASP.NET MVC code, just likewe’ve been writing throughout this tutorial. It’s intended to save you the time you’d spend on writingboilerplate controller code and creating the strongly typed views manually, but this isn’t the kind of generatedcode you may have seen prefaced with dire warnings in comments about how you mustn’t change the code.This is your code, and you’re expected to change it.So, let’s start with a quick edit to the StoreManager Index view (/Views/StoreManager/Index.cshtml). This viewwill display a table which lists the Albums in our store with Edit / Details / Delete links, and includes the Album’spublic properties. We’ll remove the AlbumArtUrl field, as it’s not very useful in this display. In <table> section of MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 55

62.
Clicking the Delete button at the bottom will delete the album and return you to the Index page, which showsthe album deleted.We’re not done with the Store Manager, but we have working controller and view code for the CRUD operationsto start from.Looking at the Store Manager Controller codeThe Store Manager Controller contains a good amount of code. Let’s go through this from top to bottom. Thecontroller includes some standard namespaces for an MVC controller, as well as a reference to our Modelsnamespace. The controller has a private instance of MusicStoreEntities, used by each of the controller actionsfor data access.using System;using System.Collections.Generic;using System.Data;using System.Data.Entity;using System.Linq;using System.Web;using System.Web.Mvc;using MvcMusicStore.Models;namespace MvcMusicStore.Controllers{ public class StoreManagerController : Controller { MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 61

63.
private MusicStoreEntities db = new MusicStoreEntities();Store Manager Index and Details actionsThe index view retrieves a list of Albums, including each album’s referenced Genre and Artist information, as wepreviously saw when working on the Store Browse method. The Index view is following the references to thelinked objects so that it can display each album’s Genre name and Artist name, so the controller is beingefficient and querying for this information in the original request.//// GET: /StoreManager/public ViewResult Index(){ var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist); return View(albums.ToList());}The StoreManager Controller’s Details controller action works exactly the same as the Store Controller Detailsaction we wrote previously - it queries for the Album by ID using the Find() method, then returns it to the view. // // GET: /StoreManager/Details/5 public ViewResult Details(int id) { Album album = db.Albums.Find(id); return View(album); }The Create Action MethodsThe Create action methods are a little different from ones we’ve seen so far, because they handle form input.When a user first visits /StoreManager/Create/ they will be shown an empty form. This HTML page will contain a<form> element that contains dropdown and textbox input elements where they can enter the album’s details.After the user fills in the Album form values, they can press the “Save” button to submit these changes back toour application to save within the database. When the user presses the “save” button the <form> will performan HTTP-POST back to the /StoreManager/Create/ URL and submit the <form> values as part of the HTTP-POST.ASP.NET MVC allows us to easily split up the logic of these two URL invocation scenarios by enabling us toimplement two separate “Create” action methods within our StoreManagerController class – one to handle theinitial HTTP-GET browse to the /StoreManager/Create/ URL, and the other to handle the HTTP-POST of thesubmitted changes.Passing information to a View using ViewBagWe’ve used the ViewBag earlier in this tutorial, but haven’t talked much about it. The ViewBag allows us to passinformation to the view without using a strongly typed model object. In this case, our Edit HTTP-GET controller MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 62

64.
action needs to pass both a list of Genres and Artists to the form to populate the dropdowns, and the simplestway to do that is to return them as ViewBag items.The ViewBag is a dynamic object, meaning that you can type ViewBag.Foo or ViewBag.YourNameHere withoutwriting code to define those properties. In this case, the controller code uses ViewBag.GenreId andViewBag.ArtistId so that the dropdown values submitted with the form will be GenreId and ArtistId, which arethe Album properties they will be setting.These dropdown values are returned to the form using the SelectList object, which is built just for that purpose.This is done using code like this:ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");As you can see from the action method code, three parameters are being used to create this object:  The list of items the dropdown will be displaying. Note that this isn’t just a string - we’re passing a list of Genres.  The next parameter being passed to the SelectList is the Selected Value. This how the SelectList knows how to pre-select an item in the list. This will be easier to understand when we look at the Edit form, which is pretty similar.  The final parameter is the property to be displayed. In this case, this is indicating that the Genre.Name property is what will be shown to the user.With that in mind, then, the HTTP-GET Create action is pretty simple - two SelectLists are added to the ViewBag,and no model object is passed to the form (since it hasn’t been created yet).//// GET: /StoreManager/Createpublic ActionResult Create(){ ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name"); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name"); return View();}HTML Helpers to display the Drop Downs in the Create ViewSince we’ve talked about how the drop down values are passed to the view, let’s take a quick look at the view tosee how those values are displayed. In the view code (/Views/StoreManager/Create.cshtml), you’ll see thefollowing call is made to display the Genre drop down.@Html.DropDownList("GenreId", String.Empty) MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 63

65.
This is known as an HTML Helper - a utility method which performs a common view task. HTML Helpers are veryuseful in keeping our view code concise and readable. The Html.DropDownList helper is provided by ASP.NETMVC, but as we’ll see later it’s possible to create our own helpers for view code we’ll reuse in our application.The Html.DropDownList call just needs to be told two things - where to get the list to display, and what value (ifany) should be pre-selected. The first parameter, GenreId, tells the DropDownList to look for a value namedGenreId in either the model or ViewBag. The second parameter is used to indicate the value to show as initiallyselected in the drop down list. Since this form is a Create form, there’s no value to be preselected andString.Empty is passed.Handling the Posted Form valuesAs we discussed before, there are two action methods associated with each form. The first handles the HTTP-GET request and displays the form. The second handles the HTTP-POST request, which contains the submittedform values. Notice that controller action has an [HttpPost] attribute, which tells ASP.NET MVC that it shouldonly respond to HTTP-POST requests.//// POST: /StoreManager/Create[HttpPost]public ActionResult Create(Album album){ if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album);}This action has four responsibilities: 1. Read the form values 2. Check if the form values pass any validation rules 3. If the form submission is valid, save the data and display the updated list 4. If the form submission is not valid, redisplay the form with validation errorsReading Form Values with Model BindingThe controller action is processing a form submission that includes values for GenreId and ArtistId (from thedrop down list) and textbox values for Title, Price, and AlbumArtUrl. While it’s possible to directly access formvalues, a better approach is to use the Model Binding capabilities built into ASP.NET MVC. When a controlleraction takes a model type as a parameter, ASP.NET MVC will attempt to populate an object of that type usingform inputs (as well as route and querystring values). It does this by looking for values whose names matchproperties of the model object, e.g. when setting the new Album object’s GenreId value, it looks for an input MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 64

66.
with the name GenreId. When you create views using the standard methods in ASP.NET MVC, the forms willalways be rendered using property names as input field names, so this the field names will just match up.Validating the ModelThe model is validated with a simple call to ModelState.IsValid. We haven’t added any validation rules to ourAlbum class yet - we’ll do that in a bit - so right now this check doesn’t have much to do. What’s important isthat this ModelStat.IsValid check will adapt to the validation rules we put on our model, so future changes tovalidation rules won’t require any updates to the controller action code.Saving the submitted valuesIf the form submission passes validation, it’s time to save the values to the database. With Entity Framework,that just requires adding the model to the Albums collection and calling SaveChanges.db.Albums.Add(album);db.SaveChanges();Entity Framework generates the appropriate SQL commands to persist the value. After saving the data, weredirect back to the list of Albums so we can see our update. This is done by returning RedirectToAction with thename of the controller action we want displayed. In this case, that’s the Index method.Displaying invalid form submissions with Validation ErrorsIn the case of invalid form input, the dropdown values are added to the ViewBag (as in the HTTP-GET case) andthe bound model values are passed back to the view for display. Validation errors are automatically displayedusing the @Html.ValidationMessageFor HTML Helper.Testing the Create FormTo test this out, run the application and browse to /StoreManager/Create/ - this will show you the blank formwhich was returned by the StoreController Create HTTP-GET method.Fill in some values and click the Create button to submit the form. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 65

67.
Handling EditsThe Edit action pair (HTTP-GET and HTTP-POST) are very similar to the Create action methods we just looked at.Since the edit scenario involves working with an existing album, the Edit HTTP-GET method loads the Albumbased on the “id” parameter, passed in via the route. This code for retrieving an album by AlbumId is the sameas we’ve previously looked at in the Details controller action. As with the Create / HTTP-GET method, the dropdown values are returned via the ViewBag. This allows us to return an Album as our model object to the view(which is strongly typed to the Album class) while passing additional data (e.g. a list of Genres) via the ViewBag.//// GET: /StoreManager/Edit/5public ActionResult Edit(int id){ Album album = db.Albums.Find(id); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 66

68.
}The Edit HTTP-POST action is very similar to the Create HTTP-POST action. The only difference is that instead ofadding a new album to the db.Albums collection, we’re finding the current instance of the Album usingdb.Entry(album) and setting its state to Modified. This tells Entity Framework that we are modifying an existingalbum as opposed to creating a new one.//// POST: /StoreManager/Edit/5[HttpPost]public ActionResult Edit(Album album){ if (ModelState.IsValid) { db.Entry(album).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album);}We can test this out by running the application and browsing to /StoreManger/, then clicking the Edit link for analbum.This displays the Edit form shown by the Edit HTTP-GET method. Fill in some values and click the Save button. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 67

69.
This posts the form, saves the values, and returns us to the Album list, showing that the values were updated.Handling DeletionDeletion follows the same pattern as Edit and Create, using one controller action to display the confirmationform, and another controller action to handle the form submission.The HTTP-GET Delete controller action is exactly the same as our previous Store Manager Details controlleraction.//// GET: /StoreManager/Delete/5public ActionResult Delete(int id){ Album album = db.Albums.Find(id); return View(album);}We display a form that’s strongly typed to an Album type, using the Delete view content template. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 68

71.
Clicking the Delete button causes the form to be posted back to the server, which executes the DeleteConfirmedaction.//// POST: /StoreManager/Delete/5[HttpPost, ActionName("Delete")]public ActionResult DeleteConfirmed(int id){ Album album = db.Albums.Find(id); db.Albums.Remove(album); db.SaveChanges(); return RedirectToAction("Index");}Our HTTP-POST Delete Controller Action takes the following actions: 1. Loads the Album by ID 2. Deletes it the album and save changes 3. Redirects to the Index, showing that the Album was removed from the listTo test this, run the application and browse to /StoreManager. Select an album from the list and click the Deletelink. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 70

72.
This displays our Delete confirmation screen.Clicking the Delete button removes the album and returns us to the Store Manager Index page, which showsthat the album has been deleted. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 71

73.
Using a custom HTML Helper to truncate textWe’ve got one potential issue with our Store Manager Index page. Our Album Title and Artist Name propertiescan both be long enough that they could throw off our table formatting. We’ll create a custom HTML Helper toallow us to easily truncate these and other properties in our Views. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 72

74.
Razor’s @helper syntax has made it pretty easy to create your own helper functions for use in your views. Openthe /Views/StoreManager/Index.cshtml view and add the following code directly after the @model line.@helper Truncate(string input, int length){ if (input.Length <= length) { @input } else { @input.Substring(0, length)<text>...</text> }}This helper method takes a string and a maximum length to allow. If the text supplied is shorter than the lengthspecified, the helper outputs it as-is. If it is longer, then it truncates the text and renders “…” for the remainder. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 73

76.
<td> @Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) | @Html.ActionLink("Details", "Details", new { id=item.AlbumId }) | @Html.ActionLink("Delete", "Delete", new { id=item.AlbumId }) </td> </tr>}</table>Now when we browse the /StoreManager/ URL, the albums and titles are kept below our maximum lengths.Note: This shows the simple case of creating and using a helper in one view. To learn more about creatinghelpers that you can use throughout your site, see my blog post: http://bit.ly/mvc3-helper-options MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 75

77.
6. Using Data Annotations for Model ValidationWe have a major issue with our Create and Edit forms: they’re not doing any validation. We can do things likeleave required fields blank or type letters in the Price field, and the first error we’ll see is from the database.We can easily add validation to our application by adding Data Annotations to our model classes. DataAnnotations allow us to describe the rules we want applied to our model properties, and ASP.NET MVC will takecare of enforcing them and displaying appropriate messages to our users.Adding Validation to our Album FormsWe’ll use the following Data Annotation attributes:  Required – Indicates that the property is a required field  DisplayName – Defines the text we want used on form fields and validation messages  StringLength – Defines a maximum length for a string field  Range – Gives a maximum and minimum value for a numeric field  Bind – Lists fields to exclude or include when binding parameter or form values to model properties  ScaffoldColumn – Allows hiding fields from editor formsNote: For more information on Model Validation using Data Annotation attributes, see the MSDN documentationat http://go.microsoft.com/fwlink/?LinkId=159063Open the Album class and add the following using statements to the top.using System.ComponentModel;using System.ComponentModel.DataAnnotations;using System.Web.Mvc;Next, update the properties to add display and validation attributes as shown below.namespace MvcMusicStore.Models{ [Bind(Exclude = "AlbumId")] public class Album { [ScaffoldColumn(false)] public int AlbumId { get; set; } [DisplayName("Genre")] public int GenreId { get; set; } [DisplayName("Artist")] public int ArtistId { get; set; } [Required(ErrorMessage = "An Album Title is required")] [StringLength(160)] public string Title { get; set; } [Required(ErrorMessage = "Price is required")] [Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")] public decimal Price { get; set; } MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 76

79.
Next, we’ll break some validation rules. Enter a price of 0 and leave the Title blank. When we click on the Createbutton, we will see the form displayed with validation error messages showing which fields did not meet thevalidation rules we have defined. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 78

80.
Testing the Client-Side ValidationServer-side validation is very important from an application perspective, because users can circumvent client-side validation. However, webpage forms which only implement server-side validation exhibit three significantproblems. 1. The user has to wait for the form to be posted, validated on the server, and for the response to be sent to their browser. 2. The user doesn’t get immediate feedback when they correct a field so that it now passes the validation rules. 3. We are wasting server resources to perform validation logic instead of leveraging the user’s browser.Fortunately, the ASP.NET MVC 3 scaffold templates have client-side validation built in, requiring no additionalwork whatsoever.Typing a single letter in the Title field satisfies the validation requirements, so the validation message isimmediately removed. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 79

82.
7. Membership and AuthorizationOur Store Manager controller is currently accessible to anyone visiting our site. Let’s change this to restrictpermission to site administrators.Adding the AccountController and ViewsOne difference between the full ASP.NET MVC 3 Web Application template and the ASP.NET MVC 3 Empty WebApplication template is that the empty template doesn’t include an Account Controller. We’ll add an AccountController by copying a few files from a new ASP.NET MVC application created from the full ASP.NET MVC 3 WebApplication template.The MvcMusicStore-Assets.zip download - which included our site design files from the beginning of the tutorial- has all the AccountController files you’ll need to add located in a folder named Code. Copy the following filesinto the same directories in our project: 1. Copy AccountController.cs in the Controllers directory 2. Copy AccountModels.cs in the Models directory 3. Create an Account directory inside the Views directory and copy all four views inChange the namespace for the Controller and Model classes so they begin with MvcMusicStore. TheAccountController class should use the MvcMusicStore.Controllers namespace, and the AccountModels classshould use the MvcMusicStore.Models namespace.Note: If you are copying these files from an empty website rather than the Assets zip, you will need to update thenamespaces to match MvcMusicStore.Controllers and MvcMusicStore.Models.The updated solution should look like the following: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 81

83.
Adding an Administrative User with the ASP.NET Configuration siteBefore we require Authorization in our website, we’ll need to create a user with access. The easiest way tocreate a user is to use the built-in ASP.NET Configuration website.Launch the ASP.NET Configuration website by clicking following the icon in the Solution Explorer. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 82

84.
This launches a configuration website. Click on the Security tab on the home screen, then click the “Enableroles” link in the center of the screen.Click the “Create or Manage roles” link. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 83

86.
Click the Back button, then click on the Create user link on the left side.Fill in the user information fields on the left using the following information:Field ValueUser Name AdministratorPassword password123!Confirm Password password123!E-mail (any e-mail address will work)Security Question (whatever you like)Security Answer (whatever you like)Note: You can of course use any password you’d like. The above password is shown as an example, and isassumed in the support forums on CodePlex. The default password security settings require a password that is 7characters long and contains one non-alphanumeric character.Select the Administrator role for this user, and click the Create User button. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 85

87.
At this point, you should see a message indicating that the user was created successfully. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 86

88.
You can now close the browser window.Role-based AuthorizationNow we can restrict access to the StoreManagerController using the [Authorize] attribute, specifying that theuser must be in the Administrator role to access any controller action in the class.[Authorize(Roles = "Administrator")]public class StoreManagerController : Controller{ // Controller code here}Note: The [Authorize] attribute can be placed on specific action methods as well as at the Controller class level.Now browsing to /StoreManager brings up a Log On dialog: MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 87

90.
8. Shopping Cart with Ajax UpdatesWe’ll allow users to place albums in their cart without registering, but they’ll need to register as guests tocomplete checkout. The shopping and checkout process will be separated into two controllers: a ShoppingCartController which allows anonymously adding items to a cart, and a Checkout Controller which handles thecheckout process. We’ll start with the Shopping Cart in this section, then build the Checkout process in thefollowing section.Adding the Cart, Order, and OrderDetail model classesOur Shopping Cart and Checkout processes will make use of some new classes. Right-click the Models folder andadd a Cart class (Cart.cs) with the following code.using System.ComponentModel.DataAnnotations;namespace MvcMusicStore.Models{ public class Cart { [Key] public int RecordId { get; set; } public string CartId { get; set; } public int AlbumId { get; set; } public int Count { get; set; } public System.DateTime DateCreated { get; set; } public virtual Album Album { get; set; } }}This class is pretty similar to others we’ve used so far, with the exception of the *Key+ attribute for the RecordIdproperty. Our Cart items will have a string identifier named CartID to allow anonymous shopping, but the tableincludes an integer primary key named RecordId. By convention, Entity Framework Code-First expects that theprimary key for a table named Cart will be either CartId or ID, but we can easily override that via annotations orcode if we want. This is an example of how we can use the simple conventions in Entity Framework Code-Firstwhen they suit us, but we’re not constrained by them when they don’t.Next, add an Order class (Order.cs) with the following code.using System.Collections.Generic;namespace MvcMusicStore.Models{ public partial class Order { public int OrderId { get; set; } public string Username { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 89

92.
Managing the Shopping Cart business logicNext, we’ll create the ShoppingCart class in the Models folder. The ShoppingCart model handles data access tothe Cart table. Additionally, it will handle the business logic to for adding and removing items from the shoppingcart.Since we don’t want to require users to sign up for an account just to add items to their shopping cart, we willassign users a temporary unique identifier (using a GUID, or globally unique identifier) when they access theshopping cart. We’ll store this ID using the ASP.NET Session class.Note: The ASP.NET Session is a convenient place to store user-specific information which will expire after theyleave the site. While misuse of session state can have performance implications on larger sites, our light use willwork well for demonstration purposes.The ShoppingCart class exposes the following methods:AddToCart takes an Album as a parameter and adds it to the user’s cart. Since the Cart table tracks quantity foreach album, it includes logic to create a new row if needed or just increment the quantity if the user has alreadyordered one copy of the album.RemoveFromCart takes an Album ID and removes it from the user’s cart. If the user only had one copy of thealbum in their cart, the row is removed.EmptyCart removes all items from a user’s shopping cart.GetCartItems retrieves a list of CartItems for display or processing.GetCount retrieves a the total number of albums a user has in their shopping cart.GetTotal calculates the total cost of all items in the cart.CreateOrder converts the shopping cart to an order during the checkout phase.GetCart is a static method which allows our controllers to obtain a cart object. It uses the GetCartId method tohandle reading the CartId from the user’s session. The GetCartId method requires the HttpContextBase so that itcan read the user’s CartId from user’s session.Here’s the complete ShoppingCart class:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcMusicStore.Models{ public partial class ShoppingCart { MusicStoreEntities storeDB = new MusicStoreEntities(); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 91

96.
Guid tempCartId = Guid.NewGuid(); // Send tempCartId back to client as a cookie context.Session[CartSessionKey] = tempCartId.ToString(); } } return context.Session[CartSessionKey].ToString(); } // When a user has logged in, migrate their shopping cart to // be associated with their username public void MigrateCart(string userName) { var shoppingCart = storeDB.Carts.Where(c => c.CartId == ShoppingCartId); foreach (Cart item in shoppingCart) { item.CartId = userName; } storeDB.SaveChanges(); } }}ViewModelsOur Shopping Cart Controller will need to communicate some complex information to its views which doesn’tmap cleanly to our Model objects. We don’t want to modify our Models to suit our views; Model classes shouldrepresent our domain, not the user interface. One solution would be to pass the information to our Views usingthe ViewBag class, as we did with the Store Manager dropdown information, but passing a lot of information viaViewBag gets hard to manage.A solution to this is to use the ViewModel pattern. When using this pattern we create strongly-typed classes thatare optimized for our specific view scenarios, and which expose properties for the dynamic values/contentneeded by our view templates. Our controller classes can then populate and pass these view-optimized classesto our view template to use. This enables type-safety, compile-time checking, and editor IntelliSense withinview templates.We’ll create two View Models for use in our Shopping Cart controller: the ShoppingCartViewModel will hold thecontents of the user’s shopping cart, and the ShoppingCartRemoveViewModel will be used to displayconfirmation information when a user removes something from their cart.Let’s create a new ViewModels folder in the root of our project to keep things organized. Right-click the project,select Add / New Folder. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 95

97.
Name the folder ViewModels.Next, add the ShoppingCartViewModel class in the ViewModels folder. It has two properties: a list of Cart items,and a decimal value to hold the total price for all items in the cart.using System.Collections.Generic;using MvcMusicStore.Models;namespace MvcMusicStore.ViewModels{ public class ShoppingCartViewModel MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 96

98.
{ public List<Cart> CartItems { get; set; } public decimal CartTotal { get; set; } }}Now add the ShoppingCartRemoveViewModel to the ViewModels folder, with the following four properties.namespace MvcMusicStore.ViewModels{ public class ShoppingCartRemoveViewModel { public string Message { get; set; } public decimal CartTotal { get; set; } public int CartCount { get; set; } public int ItemCount { get; set; } public int DeleteId { get; set; } }}The Shopping Cart ControllerThe Shopping Cart controller has three main purposes: adding items to a cart, removing items from the cart, andviewing items in the cart. It will make use of the three classes we just created: ShoppingCartViewModel,ShoppingCartRemoveViewModel, and ShoppingCart. As in the StoreController and StoreManagerController,we’ll add a field to hold an instance of MusicStoreEntities.Add a new Shopping Cart controller to the project using the Empty controller template. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 97

101.
However, instead of using an Html.ActionLink to remove items from the cart, we’ll use jQuery to “wire up” theclick event for all links in this view which have the HTML class RemoveLink. Rather than posting the form, thisclick event handler will just make an AJAX callback to our RemoveFromCart controller action. TheRemoveFromCart returns a JSON serialized result, which is automatically passed to the JavaScript methodspecified in our AjaxOptions OnSuccess parameter – handleUpdate in this case. The handleUpdate Javascriptfunction parses the JSON results and performs four quick updates to the page using jQuery: 1. Removes the deleted album from the list 2. Updates the cart count in the header 3. Displays an update message to the user 4. Updates the cart total priceSince the remove scenario is being handled by an Ajax callback within the Index view, we don’t need anadditional view for RemoveFromCart action. Here is the complete code for the /ShoppingCart/Index view:@model MvcMusicStore.ViewModels.ShoppingCartViewModel@{ ViewBag.Title = "Shopping Cart"; MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 100

110.
9. Registration and CheckoutIn this section, we will be creating a CheckoutController which will collect the shopper’s address and paymentinformation. We will require users to register with our site prior to checking out, so this controller will requireauthorization.Users will navigate to the checkout process from their shopping cart by clicking the “Checkout” button.If the user is not logged in, they will be prompted to. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 109

112.
Once they have filled the form and submitted the order, they will be shown the order confirmation screen. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 111

113.
Attempting to view either a non-existent order or an order that doesn’t belong to you will show the Error view. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 112

114.
Migrating the Shopping CartWhile the shopping process is anonymous, when the user clicks on the Checkout button, they will be required toregister and login. Users will expect that we will maintain their shopping cart information between visits, so wewill need to associate the shopping cart information with a user when they complete registration or login.This is actually very simple to do, as our ShoppingCart class already has a method which will associate all theitems in the current cart with a username. We will just need to call this method when a user completesregistration or login.Open the AccountController class that we added when we were setting up Membership and Authorization. Adda using statement referencing MvcMusicStore.Models, then add the following MigrateShoppingCart method:private void MigrateShoppingCart(string UserName){ // Associate shopping cart items with logged-in user var cart = ShoppingCart.GetCart(this.HttpContext); cart.MigrateCart(UserName); Session[ShoppingCart.CartSessionKey] = UserName;}Next, modify the LogOn post action to call MigrateShoppingCart after the user has been validated, as shownbelow://// POST: /Account/LogOn[HttpPost]public ActionResult LogOn(LogOnModel model, string returnUrl){ if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { MigrateShoppingCart(model.UserName); FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 &&returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided isincorrect."); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 113

116.
First, add the Authorize attribute above the Controller class declaration to require users to register beforecheckout:namespace MvcMusicStore.Controllers{ [Authorize] public class CheckoutController : ControllerNote: This is similar to the change we previously made to the StoreManagerController, but in that case theAuthorize attribute required that the user be in an Administrator role. In the Checkout Controller, we’re requiringthe user be logged in but aren’t requiring that they be administrators.For the sake of simplicity, we won’t be dealing with payment information in this tutorial. Instead, we areallowing users to check out using a promotional code. We will store this promotional code using a constantnamed PromoCode.As in the StoreController, we’ll declare a field to hold an instance of the MusicStoreEntities class, namedstoreDB. In order to make use of the MusicStoreEntities class, we will need to add a using statement for theMvcMusicStore.Models namespace. The top of our Checkout controller appears below.using System;using System.Linq;using System.Web.Mvc;using MvcMusicStore.Models;namespace MvcMusicStore.Controllers MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 115

117.
{ [Authorize] public class CheckoutController : Controller { MusicStoreEntities storeDB = new MusicStoreEntities(); const string PromoCode = "FREE";The CheckoutController will have the following controller actions:AddressAndPayment (GET method) will display a form to allow the user to enter their information.AddressAndPayment (POST method) will validate the input and process the order.Complete will be shown after a user has successfully finished the checkout process. This view will include theuser’s order number, as confirmation.First, let’s rename the Index controller action (which was generated when we created the controller) toAddressAndPayment. This controller action just displays the checkout form, so it doesn’t require any modelinformation.//// GET: /Checkout/AddressAndPaymentpublic ActionResult AddressAndPayment(){ return View();}Our AddressAndPayment POST method will follow the same pattern we used in the StoreManagerController: itwill try to accept the form submission and complete the order, and will re-display the form if it fails.After validating the form input meets our validation requirements for an Order, we will check the PromoCodeform value directly. Assuming everything is correct, we will save the updated information with the order, tell theShoppingCart object to complete the order process, and redirect to the Complete action.//// POST: /Checkout/AddressAndPayment[HttpPost]public ActionResult AddressAndPayment(FormCollection values){ var order = new Order(); TryUpdateModel(order); try { if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return View(order); } MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 116

121.
This view will make use of two of the techniques we looked at while building the StoreManagerEdit view:  We will use Html.EditorForModel() to display form fields for the Order model  We will leverage validation rules using an Order class with validation attributesWe’ll start by updating the form code to use Html.EditorForModel(), followed by an additional textbox for thePromo Code. The complete code for the AddressAndPayment view is shown below.@model MvcMusicStore.Models.Order@{ ViewBag.Title = "Address And Payment";}<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript"></script>@using (Html.BeginForm()) { MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 120

124.
Okay, we’ve done most of the hard work for the checkout process; we just have a few odds and ends to finish.We need to add two simple views, and we need to take care of the handoff of the cart information during thelogin process.Adding the Checkout Complete viewThe Checkout Complete view is pretty simple, as it just needs to display the Order ID. Right-click on theComplete controller action and add a view named Complete which is strongly typed as an int. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 123

125.
Now we will update the view code to display the Order ID, as shown below.@model int@{ ViewBag.Title = "Checkout Complete";}<h2>Checkout Complete</h2><p>Thanks for your order! Your order number is: @Model</p><p>How about shopping for some more music in our @Html.ActionLink("store", "Index", "Home")</p>Updating The Error viewThe default template includes an Error view in the Shared views folder so that it can be re-used elsewhere in thesite. This Error view contains a very simple error and doesn’t use our site Layout, so we’ll update it. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 124

126.
Since this is a generic error page, the content is very simple. We’ll include a message and a link to navigate tothe previous page in history if the user wants to re-try their action.@{ ViewBag.Title = "Error";}<h2>Error</h2><p>Were sorry, weve hit an unexpected error. <a href="javascript:history.go(-1)">Click here</a> if youd like to go back and try that again.</p> MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 125

127.
10. Final updates to Navigation and Site DesignWe’ve completed all the major functionality for our site, but we still have some features to add to the sitenavigation, the home page, and the Store Browse page.Creating the Shopping Cart Summary Partial ViewWe want to expose the number of items in the user’s shopping cart across the entire site.We can easily implement this by creating a partial view which is added to our Site.master.As shown previously, the ShoppingCart controller includes a CartSummary action method which returns a partialview://// GET: /ShoppingCart/CartSummary[ChildActionOnly]public ActionResult CartSummary(){ var cart = ShoppingCart.GetCart(this.HttpContext); ViewData["CartCount"] = cart.GetCount(); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 126

128.
return PartialView("CartSummary");}To create the CartSummary partial view, right-click on the Views/ShoppingCart folder and select Add View.Name the view CartSummary and check the “Create a partial view” checkbox as shown below.The CartSummary partial view is really simple - it’s just a link to the ShoppingCart Index view which shows thenumber of items in the cart. The complete code for CartSummary.cshtml is as follows:@Html.ActionLink("Cart (" + ViewData["CartCount"] + ")", "Index", "ShoppingCart", new { id = "cart-status" })We can include a partial view in any page in the site, including the Site master, by using the Html.RenderActionmethod. RenderAction requires us to specify the Action Name (“CartSummary”) and the Controller Name(“ShoppingCart”) as below.@Html.RenderAction("CartSummary", "ShoppingCart") MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 127

129.
Before adding this to the site Layout, we will also create the Genre Menu so we can make all of our Site.masterupdates at one time.Creating the Genre Menu Partial ViewWe can make it a lot easier for our users to navigate through the store by adding a Genre Menu which lists allthe Genres available in our store.We will follow the same steps also create a GenreMenu partial view, and then we can add them both to the Sitemaster. First, add the following GenreMenu controller action to the StoreController://// GET: /Store/GenreMenu[ChildActionOnly]public ActionResult GenreMenu(){ var genres = storeDB.Genres.ToList(); return PartialView(genres);} MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 128

130.
This action returns a list of Genres which will be displayed by the partial view, which we will create next.Note: We have added the [ChildActionOnly] attribute to this controller action, which indicates that we only wantthis action to be used from a Partial View. This attribute will prevent the controller action from being executed bybrowsing to /Store/GenreMenu. This isn’t required for partial views, but it is a good practice, since we want tomake sure our controller actions are used as we intend. We are also returning PartialView rather than View,which lets the view engine know that it shouldn’t use the Layout for this view, as it is being included in otherviews.Right-click on the GenreMenu controller action and create a partial view named GenreMenu which is stronglytyped using the Genre view data class as shown below.Update the view code for the GenreMenu partial view to display the items using an unordered list as follows.@model IEnumerable<MvcMusicStore.Models.Genre><ul id="categories"> @foreach (var genre in Model) MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 129

132.
@model MvcMusicStore.Models.Genre@{ ViewBag.Title = "Browse Albums";}<div class="genre"> <h3><em>@Model.Name</em> Albums</h3> <ul id="album-list"> @foreach (var album in Model.Albums) { <li> <a href="@Url.Action("Details", new { id = album.AlbumId })"> <img alt="@album.Title" src="@album.AlbumArtUrl" /> <span>@album.Title</span> </a> </li> } </ul></div>Here we are making use of Url.Action rather than Html.ActionLink so that we can apply special formatting to thelink to include the album artwork.Note: We are displaying a generic album cover for these albums. This information is stored in the database and iseditable via the Store Manager. You are welcome to add your own artwork.Now when we browse to a Genre, we will see the albums shown in a grid with the album artwork. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 131

133.
Updating the Home Page to show Top Selling AlbumsWe want to feature our top selling albums on the home page to increase sales. We’ll make some updates to ourHomeController to handle that, and add in some additional graphics as well.First, we’ll add a navigation property to our Album class so that EntityFramework knows that they’re associated.The last few lines of our Album class should now look like this: public virtual Genre Genre { get; set; } public virtual Artist Artist { get; set; } public virtual List<OrderDetail> OrderDetails { get; set; } }}Note: This will require adding a using statement to bring in the System.Collections.Generic namespace. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 132

134.
First, we’ll add a storeDB field and the MvcMusicStore.Models using statements, as in our other controllers.Next, we’ll add the following method to the HomeController which queries our database to find top sellingalbums according to OrderDetails.private List<Album> GetTopSellingAlbums(int count){ // Group the order details by album and return // the albums with the highest count return storeDB.Albums .OrderByDescending(a => a.OrderDetails.Count()) .Take(count) .ToList();}This is a private method, since we don’t want to make it available as a controller action. We are including it inthe HomeController for simplicity, but you are encouraged to move your business logic into separate serviceclasses as appropriate.With that in place, we can update the Index controller action to query the top 5 selling albums and return themto the view.public ActionResult Index(){ // Get most popular albums var albums = GetTopSellingAlbums(5); return View(albums);}The complete code for the updated HomeController is as shown below.using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using MvcMusicStore.Models;namespace MvcMusicStore.Controllers{ public class HomeController : Controller { // // GET: /Home/ MusicStoreEntities storeDB = new MusicStoreEntities(); public ActionResult Index() { // Get most popular albums var albums = GetTopSellingAlbums(5); MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 133

135.
return View(albums); } private List<Album> GetTopSellingAlbums(int count) { // Group the order details by album and return // the albums with the highest count return storeDB.Albums .OrderByDescending(a => a.OrderDetails.Count()) .Take(count) .ToList(); } }}Finally, we’ll need to update our Home Index view so that it can display a list of albums by updating the Modeltype and adding the album list to the bottom. We will take this opportunity to also add a heading and apromotion section to the page.@model List<MvcMusicStore.Models.Album>@{ ViewBag.Title = "ASP.NET MVC Music Store";}<div id="promotion"></div><h3><em>Fresh</em> off the grill</h3><ul id="album-list"> @foreach (var album in Model) { <li><a href="@Url.Action("Details", "Store", new { id = album.AlbumId })"> <img alt="@album.Title" src="@album.AlbumArtUrl" /> <span>@album.Title</span> </a> </li> }</ul>Now when we run the application, we’ll see our updated home page with top selling albums and ourpromotional message. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 134