StoreManager Edit Error

Mar 21, 2011 at 1:59 AM

When I try to edit an album I am getting an error stating InvalidOperationException: Sequence contains no elements.  What am I doing wrong?

Here is my code.  The error is occurring at var album = storeDB.Albums.Single.....

        public ActionResult Edit(int id, FormCollection formValues)
            var album = storeDB.Albums.Single(a => a.ArtistId == id);

                // Save Album

                UpdateModel(album, "Album");
                return RedirectToAction("Index");
                // Error Occurred - so redisplay the form

                var viewModel = new StoreManagerViewModel
                    Album = album,
                    Genres = storeDB.Genres.ToList(),
                    Artists = storeDB.Artists.ToList()

                return View(viewModel);

Mar 25, 2011 at 7:38 AM

From the code, it looks like you're using an older release of the tutorial. I recommend using the latest release (version 2) if possible.

There's a typo in that line:

var album = storeDB.Albums.Single(a => a.ArtistId == id);

You're looking up an album, but you're matching on ArtistId. That line should read as follows:

var album = storeDB.Albums.Single(a => a.AlbumId == id);