6
Vote

Unable to Update Album

description

If you try and update an album, you get a concurrency error. For all albums, regardless of whether they are the pre-seeded albums or ones that you add yourself.

comments

monkimo wrote Apr 26, 2011 at 10:44 PM

I think I've figured out the issue (I could be wrong, this is the first time I've tried EF).

In the Album class, you've defined [Bind(Exclude = "AlbumId")] on the class, which means that the code won't try and bind the AlbumId from the form. Which is fine, except that on the edit page, you're passing in a populated Album, which is presumably being populated using binding, which is of course ignoring the Album Id, so the AlbumId of the object passed into the edit method, is always 0, which throws a misleading concurrency error, because no rows are affected, because there's no album with ID of 0.

If you remove the bind/exclude attribute, then the edit page works as expected.

jongalloway wrote Apr 29, 2011 at 1:01 AM

Which version of the tutorial are you using?

monkimo wrote Apr 30, 2011 at 4:18 PM

It was using the latest version (3.0). If you download the finished code and run it, and try to edit an album, you should see the error.

xiazhichen wrote May 1, 2011 at 6:09 AM

This problem has really troubled me for a long, thank u for the solution

taljaardjcf wrote Jun 4, 2011 at 11:41 PM

Hi there after removing the exlude binding I still had the error, I ended up adding the recordID field to my view, as a hidden field , and the recordID was passed through as expected

consiliumt wrote Jun 21, 2011 at 11:32 AM

I posted this question on SO which is the same problem. I thought it was something wrong with my refactoring.

http://stackoverflow.com/questions/6410813/model-id-changes-when-past-to-the-httppost-method

g24 wrote Aug 5, 2011 at 2:59 PM

There is another solution for this here:
http://mvcmusicstore.codeplex.com/discussions/264765

which involves addding the index of the album to be edited as the second parameter to this function in StoreManagerController:
    [HttpPost]
    public ActionResult Edit(Album album, int id)
    {
        if (ModelState.IsValid)
        {
            album.AlbumId = id;
            db.Entry(album).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
        ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
        return View(album);
    }

New to this, so not sure which is the best fix.
Any comment?

g24 wrote Aug 5, 2011 at 2:59 PM

There is another solution for this here:
http://mvcmusicstore.codeplex.com/discussions/264765

which involves addding the index of the album to be edited as the second parameter to this function in StoreManagerController:
    [HttpPost]
    public ActionResult Edit(Album album, int id)
    {
        if (ModelState.IsValid)
        {
            album.AlbumId = id;
            db.Entry(album).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
        ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
        return View(album);
    }

New to this, so not sure which is the best fix.
Any comment?

g24 wrote Aug 5, 2011 at 3:01 PM

ah, new posts at the top. Sorry for doublepost

RajGuptaWa wrote Apr 29, 2013 at 5:45 PM

Monkimo's solution that worked for me was to simply remove [Bind(Exclude = "AlbumId")] in Album class.

To Jon,

Yes, this is the current version of your app, as of April 2013 that still has bug on edit. Obviously, not a lot of testing done here... but since you are letting your users test , you should at least address / correct the bugs you find.

I dont expect comments to offer explanation of why the [Bind(Exclude = "AlbumId")] was included in the first place. But you could/should.

The assumption is that a tutorial works. Maybe your approach is to add a lot of bugs, and let users hone their debugging skills. If that is your approach, fine - a word of warning would have saved a lot of people wasted hours.