Hacking your head while learning MVC through the MusicStore MVC. Received this error for Model.Artist.Name for the detail view page.
My Storecontroller Details method must be accurate.
public ActionResult Details(int id) {
And this is how I am inferring the view
<li>Price : <%=Model.Price %></li> <li>Artist : <%=Model.Artist.Name%></li>
Works great for the price, and it only shows an error for Model.Genre.Name and Artist.Name. I suspect that declaring these properties in the sampledata.cs file caused this problem.
Artist = artists.Single(a => a.Name == "Aaron Copland & London Symphony Orchestra")
But my knowledge of this is too weak to come up with something. Please help.
Ok, the value is extracted from a class file that looks 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")} } }
Notice how the value is assigned, I can access Model.Title fine (where the model is like a short hand), but Model.Artist.Name caused this error.
solvable
Ok, let this work by adding a virtual keyword to the Artist and Genre declaration in the Album class. But I'm still not sure what will happen, and would like someone to want to shed light.
In my Album class, before it is resolved, it looks like
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 resolved by adding a virtual keyword
public virtual Artist Artist { get; set; }
Somehow, I still could not justify what was happening, and would like to know more. Anyone have to explain?
It happens so 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) {
Finally, 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")} } }