Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

model view controller - ASP.NET MVC Object reference not set to an instance

I have a simple application that allows you to view a list of music genres and then add/edit/remove music from a genre. My issue is that when I try to edit a track, I am given an error that no value has been passed to show the genres ID or artists ID.

When I enter breakpoints, I get an genre ID of 1 all the way up until I click the edit track button and then the value being passed into the controller that updates the music becomes NULL so I keep getting the error - Object reference not set to an instance of an object

The error is appearing on the Music Controller at;

Genre genre = genreService.GetGenre(music);                                                              
musicGenreArtist.Genre = genre.ID;`

When I create breakpoints on genreService.GetGenre(music) to see the ID of genre, it states 1 but after trying to edit it becomes null again.

Can someone explain why the ID value doesn't pass through to the update form?

Genre model class:

public class Genre
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Music> Musics { get; set; }
   
}

Music Service class:

    public void UpdateMusic(MusicGenreArtist musicGenreArtist, int musicID, string userID)
    {
        using (var context = new ForestContext())
        {
            Music _music = musicDAO.GetMusic(musicID, context);
            Genre _genre = genreDAO.GetGenre(_music, context);
            Artist _artist = artistDAO.GetArtist(_music, context);
            _music.Title = musicGenreArtist.Title;
            _music.num_track = musicGenreArtist.num_track;
            _music.duration = musicGenreArtist.duration;
            _music.DateReleased = musicGenreArtist.DateReleased;
            _music.Price = musicGenreArtist.Price;
            _music.Image = musicGenreArtist.Image;
            musicDAO.UpdateMusic(_music, context);
            Music musicToBeRemoved = musicDAO.GetMusic(musicID, context);
            if (musicGenreArtist.Genre != _genre.ID)
            {
                genreDAO.RemoveMusicFromCollection(musicToBeRemoved, _genre, context);
                Genre newGenre = genreDAO.GetGenre(musicGenreArtist.Genre, context);
                genreDAO.AddMusicToCollection(_music, newGenre, context);
            }
            if (musicGenreArtist.Artist != _artist.ID)
            {
                artistDAO.RemoveMusicFromCollection(musicToBeRemoved, _artist, context);
                Artist newArtist = artistDAO.GetArtist(musicGenreArtist.Artist, context);
                artistDAO.AddMusicToCollection(_music, newArtist, context);
            }


        }
    }

GetGenre(music) action:

    public Genre GetGenre(Music music, ForestContext context)
    {
        IList<Genre> genres = GetGenres(context);
        for (int i = 0; i < genres.Count; i++)
        {
            if (genres[i].Musics.Contains<Music>(music))
            {
                return genres[i];
            }
        }
        return null;
    }

Genre Service class:

    public Genre GetGenre(Music music)
    {
        using (var context = new ForestContext())

        {
            return genreDAO.GetGenre(music, context);
        }

    }

Controller that updates music:

    public ActionResult UpdateMusic(int musicID)
    {
        MusicGenreArtist musicGenreArtist = new MusicGenreArtist();
        Music music = musicService.GetMusic(musicID);
        musicGenreArtist.Title = music.Title;
        musicGenreArtist.num_track = music.num_track;
        musicGenreArtist.duration = music.duration;
        musicGenreArtist.DateReleased = music.DateReleased;
        musicGenreArtist.Price = music.Price;
        musicGenreArtist.Image = music.Image;
        Genre genre = genreService.GetGenre(music);
        musicGenreArtist.Genre = genre.ID;
        Artist artist = artistService.GetArtist(music);
        musicGenreArtist.Artist = artist.ID;

        Helper helper = new Helper();

        ViewBag.genreList = helper.GetGenreDropDown();
        ViewBag.artistList = helper.GetArtistDropDown();
        return View(musicGenreArtist);
    }

DAO for music:

    public void UpdateMusic(Music music, ForestContext context)
    {
        Music productToUpdate = GetMusic(music.ID, context);
        context.Entry(productToUpdate).CurrentValues.SetValues(music);
        context.SaveChanges();
    }

HTML action to get to edit page:

@Html.ActionLink("Edit", "UpdateMusic", new { musicID=item.ID, Controller = "AddMusic" })
question from:https://stackoverflow.com/questions/65832895/asp-net-mvc-object-reference-not-set-to-an-instance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...