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.....

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

            try
            {
                // Save Album

                UpdateModel(album, "Album");
                storeDB.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                // Error Occurred - so redisplay the form

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

                return View(viewModel);
            }
        }

Coordinator
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);