Genre & Artist name not showing up on StoreManager/Details

Oct 23, 2011 at 6:16 AM
Edited Oct 23, 2011 at 11:11 PM

I'm following the instructions in the v3.0 tutorial, and I'm on page 62.

What I'm having trouble figuring out is why the artist name and genre name don't appear in the details. (even in the screen shot on pg 62 they don't appear).

I'm new to asp.net MVC, so maybe I'm missing something.

I have this code in the StoreManagerController.cs

 

        public ViewResult Details(int id)
        {
            Album album = db.Albums.Find(id);
            return View(album);
        }

 

I have this code in the Details.cshtml

 

    <div class="display-label">Artist</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Artist.Name)
    </div>

    <div class="display-label">Genre</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Genre.Name)
    </div>

I have this code in the Album.cs

    public class Album
    {
        public int ArtistId { get; set; }
        public int GenreId { get; set; }
        public int AlbumId { get; set; }
        public decimal Price { get; set; }
        public string AlbumArtUrl { get; set; }
        public string Title { get; set; }
        public Genre Genre { get; set; }
        public Artist Artist { get; set; }
    }

I'm using VS2010

The right values appear in the edit view, so I'm not sure what's going on here.

Oct 23, 2011 at 11:26 PM

After doing some more research on the internet, I changed my Album.cs class to look like this now.

    public class Album
    {
        public int ArtistId { get; set; }
        public int GenreId { get; set; }
        public int AlbumId { get; set; }
        public decimal Price { get; set; }
        public string AlbumArtUrl { get; set; }
        public string Title { get; set; }
        public virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }

I added the two virtual words.  I'm still not sure what it's doing, but at least it works...guess I'll keep on researching.

Coordinator
Oct 24, 2011 at 11:14 PM

Yes - keep with the tutorial, those virtual keywords get added to the model on page 78.

Exactly as SumOfDavid says, those tell Entity Framework to lazy load.