Making a button to retrieve all albums from a certain artist

Jun 6, 2012 at 3:23 PM

I am interested in making a button, within the details of the store view. So far I have this:

(details.cshtml)

<p class="button">
        @Html.ActionLink("More Songs from " + @Model.Artist.Name + " Here", "BrowseArtist", "StoreController", new { id = Model.AlbumId }, "")
    </p>

 

(StoreController.cs)

        public ActionResult BrowseArtist(string artist)
        {
            var artistModel = storeDB.Artists.Include("Albums")
                .Single(g => g.Name == artist);

            return View(artistModel);

 

(BrowseArtist.cshtml)

@model MyMusicStore.Models.Artist
@{
    ViewBag.Title = "BrowseArtist";
}
<div class="genre">
    <h3><em>@Model.Albums</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>

Coordinator
Jun 12, 2012 at 7:33 AM

It looks like you're off to a good start. Your controller should be using a Where query rather than a Single, as you're looking for more than one Album:

        public ActionResult BrowseArtist(string artist)
        {
            var artistModel = storeDB.Albums
                .Where(g => g.Artist.Name == artist);

            return View(artistModel);
        }