Drop Down Lists and When you need to use .Net 3.5 and Older EF

Jan 10, 2012 at 1:28 PM

Hi,

 

I'm new here and new(ish) to C#, but a long time C++ dev. I started going through this excellent tutorial, as I'll need to help build an ASP.NET app using MVC. It's on a very out of date intranet, and we'll not be able to use EF 4 and .NET 4.

 

Anyway, I had real problems today with the editing of albums as the drop down list values for artist and genre were not being saved. I noticed that there have been a few older posts on this - the answer being genereally "it's because older EF doesn't include FK id, just the object, please use a newer version"

 

Sooooo after about 4 hours now, I've got it to work, and it's pretty simple, and I thought I'd post it here in case anyone is in the same situation

First, you need to make a partial class of Album to contain the FK properties:

 

 public partial class Album
   {
       public int ArtistID
       {
           get
           {
               if (this.Artist != null)
                   return this.Artist.ArtistId;
               else
                   return 0;
           }
           set
           {
               this.ArtistReference.EntityKey = new System.Data.EntityKey("MusicStoreEntities2.Album", "ArtistID", value);
           }

       }


       public int GenreID
       {
           get
           {
               if (this.Genre != null)
                   return this.Genre.GenreId;
               else
                   return 0;
           }
           set
           {
               this.GenreReference.EntityKey = new System.Data.EntityKey("MusicStoreEntities2.Album", "GenreID", value);
           }

       }
Then in the StoreManagerController, you need to extract the album as before, but this time from the formcollection, update the Album with the Genre and Artist objects, drawn from the DB using the selected ID's.

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

            string GenreKey = formValues["album.GenreId"];
            string ArtistKey = formValues["album.ArtistId"];

            int GInt = Convert.ToInt32(GenreKey);
            int AInt = Convert.ToInt32(GenreKey);
            
            var GenrePicked = storeDB.Genres.First(g => g.GenreId == GInt);
            var ArtistPicked = storeDB.Artists.First(g => g.ArtistId == AInt);      
           

            album.Artist = ArtistPicked;
            album.Genre = GenrePicked;

            try
            {
                TryUpdateModel(album, "Album");
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                var viewModel = new StoreManagerViewModel
                {
                    album = album,
                    Genres = storeDB.Genres.ToList(),
                    Artists = storeDB.Artists.ToList()
                };
                return View(viewModel);
            }

Also, I used TryUpdateModel instead of UpdateModel as it doesn't throw an exception when unexpected properties are found.

 

I'm sure there's a better way of doing this, but I couldn't find an answer in the other posts.

 

HTH

 

Richard

Jan 10, 2012 at 1:54 PM

... of course, using the FormCollection with the name strings makes maintenance a headache, so hopefully someone with a better solution will chime in!