Select from @Model.Count() genres:
and | elements surrounding AlbumArtUrl references, as indicated by the highlighted lines below:
The modified view code will appear as follows: @model IEnumerable MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 56 @{ ViewBag.Title = "Index"; } Index@Html.ActionLink("Create New", "Create")
A first look at the Store Manager Now run the application and browse to /StoreManager/. This displays the Store Manager Index we just modified, showing a list of the albums in the store with links to Edit, Details, and Delete. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 57 Clicking the Edit link displays an edit form with fields for the Album, including dropdowns for Genre and Artist. MVC Music Store Tutorial v3.0b (MVC 3 Tools Update release) – http://mvcmusicstore.codeplex.com Tutorial under Creative Commons Attribution 3.0 License Page 58 Click the “Back to List” link at the bottom, then click on the Details link for an Album. This displays the detail information for an individual 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 59 Again, click the Back to List link, then click on a Delete link. This displays a confirmation dialog, showing the album details and asking if we’re sure we want to delete 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 60 Clicking the Delete button at the bottom will delete the album and return you to the Index page, which shows the album deleted. We’re not done with the Store Manager, but we have working controller and view code for the CRUD operations to start from. Looking at the Store Manager Controller code The Store Manager Controller contains a good amount of code. Let’s go through this from top to bottom. The controller includes some standard namespaces for an MVC controller, as well as a reference to our Models namespace. The controller has a private instance of MusicStoreEntities, used by each of the controller actions for data access. using using using using using using using using System; System.Collections.Generic; System.Data; System.Data.Entity; System.Linq; System.Web; System.Web.Mvc; 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 private MusicStoreEntities db = new MusicStoreEntities(); Store Manager Index and Details actions The index view retrieves a list of Albums, including each album’s referenced Genre and Artist information, as we previously saw when working on the Store Browse method. The Index view is following the references to the linked objects so that it can display each album’s Genre name and Artist name, so the controller is being efficient 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 Details action 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 Methods The 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 |
---|