Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:50:58+00:00 2026-05-23T06:50:58+00:00

Cracking my head while learning MVC through MVC MusicStore . Got this error for

  • 0

Cracking my head while learning MVC through MVC MusicStore . Got this error for Model.Artist.Name for Details View page .

My Storecontroller Details method should be fine .

    public ActionResult Details(int id)
    {
        //returns and albums searched from the id 
        var albums = storeDB.Albums.Find(id);
        return View(albums);
    }

And this is how I output the view

    <li>Price : <%=Model.Price %></li>
    <li>Artist : <%=Model.Artist.Name%></li>

Works fine for the price , and it only show error for Model.Genre.Name and Artist.Name . I am suspecting the declaration for these properties in sampledata.cs caused this issue

    Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra")

But my knowledge of this is too weak to make up anything . Please help .

Alright , the value is fetched from a class file looking like this

    protected override void Seed(MusicStoreEntities context)
    {
        var artists = new List<Artist>
        {
            new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
            etcetc
        }
         new List<Album>
        {
            new Album { Title = "ABC", Artist= artists.Single(g => g.Name == "Test")}
        }
    }

Take notice on how the value are being assigned , I can access Model.Title fine (where Model as the short hand) , but Model.Artist.Name caused me that error .

Solved

Alright , got it to work by adding virtual keyword to the Artist and Genre declaration in Album class . But I am still not sure what happens and would like to have anyone care to shed some lights .

In my Album class , before it is solved it looks like this

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; }
}

The error caused by Genre and Artist is solved by adding Virtual keyword

    public virtual Artist Artist { get; set; }

Somehow , I still couldn’t justify what happens and wish to learn more . Anyone care to explain ?

It goes like this , Album.cs

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 virtual Genre Genre { get; set; }
        public virtual Artist Artist { get; set; }
    }
}

using EF MusicStore.cs

namespace MvcMusicStore.Models
{
    //represent entity framework , handle create ,read , update and del ops 
    public class MusicStoreEntities : DbContext
    {
        public DbSet<Album> Albums { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Artist> Artists { get; set; }

    }
}

and implemented in StoreController.cs

    MusicStoreEntities storeDB = new MusicStoreEntities();

    public ActionResult Details(int id)
    {
        //returns and albums searched from the id 
        var albums = storeDB.Albums.Find(id);
        return View(albums);
    }

Finally the sample data

protected override void Seed(MusicStoreEntities context)
{
    var artists = new List<Artist>
    {
        new Artist { Name = "Aaron Copland & London Symphony Orchestra" },
        etcetc
    }
     new List<Album>
    {
        new Album { Title = "ABC", Artist= artists.Single(g => g.Name == "Test")}
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:50:59+00:00Added an answer on May 23, 2026 at 6:50 am

    Alright , got it to work by adding virtual keyword to the Artist and Genre declaration in Album class . But I am still not sure what happens and would like to have anyone care to shed some lights .

    See above .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i been cracking my head over this memory leak.. my datasource is mutabledictionary..that i
I've been cracking my head on this one for weeks now. What happens if
Hi I've googled for this...it's been 2 days...this null stuff really cracking my head!
I'm new to C/C++ and I've been cracking my head but still got no
This is a nut I'm cracking these days Application I'm working on has some
While reading one book named Cracking the coding interview by Gayle Laakmann , i
This really has me scratching my head. Namely because it only happens in IE,
I was reading this book entitled, Cracking the Coding Interview by Laakman. There is
I've been cracking heads on how to achieve this in C++: string format =
I was wondering what the most computationally efficient Python way of cracking this problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.