Quantcast
Channel: Internet Explorer Web Development forum
Viewing all articles
Browse latest Browse all 3527

MVC - Object Reference Song Error

$
0
0

Hi guys, I'm encountering a error stating "Object reference not set to an instance of an object" in my code in relation to when I try to make songs counted.

I believe the error lies either in My Model or View

This is my Model where seeding is done for Artists and Songs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcMusicApp.Models
{

        public class DbInitialiser : DropCreateDatabaseAlways<ArtistDb>
        {
            protected override void Seed(ArtistDb context)


        {
            var seedArtist = new List<Artist>()
            {
                new Artist() {ArtistName = "Eminen" , ArtistDescription = "Age 42" , Songs =
                    new List<Song>()
                    {
                        new Song() {SongName = "Slim Shady"}
                    }

                },
                new Artist() {ArtistName = "50 cent", ArtistDescription = "Age 39"
                }
            };
            seedArtist.ForEach(Art => context.Artists.Add(Art));
            context.SaveChanges();
        }
        }

        public class ArtistDb : DbContext
        {
            public DbSet<Artist> Artists { get; set; }
            public DbSet<Song> Songs { get; set; }

            public ArtistDb()
                : base("ArtistDb")
            {

            }
        }


        public class Artist
        {
            public int ArtistId { get; set; }
            public string ArtistName { get; set; }
            public string ArtistDescription { get; set; }
            public List<Song> Songs { get; set; }
        }
        //1 artist can have many songs
        public class Song
        {
            public int SongId { get; set; }
            public string SongName { get; set; }
            public Artist Artist { get; set; }
        }

}

 

My View where the Error Occurs:

@model IEnumerable<MvcMusicApp.Models.Artist>

@{
    ViewBag.Title = "Index";
}<div class="row" style="margin-top: 120px"><div class="panel panel-success"><div class="panel-heading lead">Artists of Blogs</div><div class="panel-body"></div><table class="table table-bordered table-hover table-striped table-condensed"><tr><th>Name</th><th>Age</th><th>No of Songs</th></tr>
            @foreach (var Art in Model)
            {<tr><td>@Art.ArtistName</td><td>@Art.ArtistDescription</td><td>@Art.Songs.Count</td>//Error Occurs here</tr>
            }</table></div></div>

Though Unlikely where the problem lies, here is the controller:

using MvcMusicApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcMusicApp.Controllers
{
    public class HomeController : Controller
    {
        private ArtistDb db = new ArtistDb();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(db.Artists.ToList());
        }

    }
}


Viewing all articles
Browse latest Browse all 3527

Trending Articles