• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1331
  • Last Modified:

How do I override the db.SaveChanges method that MVC scaffolding built?

Hi,

I'm new to MVC.  I built a model and let MVC do scaffolding.  It works great, but I need to know how to override the @Html.EditorFor(model => model.PROPERTY) behavior...

For instance, I created a dropdown on my Edit page, like so:
Album: @Html.DropDownList("ddAlbum", @Model.Album)
Where the MVC scaffolding had:
@Html.EditorFor(model => model.Album)

How do I save the value from my dropdownlist to save in the db.SaveChanges() method?  In the following scaffolded code:

            if (ModelState.IsValid)
            {              
                db.Entry(song).State = EntityState.Modified;              
                db.SaveChanges();
                return RedirectToAction("Index");
            }
0
JT_SIRO
Asked:
JT_SIRO
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
Is there a post back that calls the db.SaveChanges?  Is that code in a controller action method?
0
 
JT_SIROAuthor Commented:
Yes, it's in the postback Edit Action of my controller.  Like so:

        //
        // POST: /Songs/Edit/5
        [HttpPost]
        public ActionResult Edit(Song song)
        {
            // UPLOAD SONG PHOTO TO C:\SongBank\Images\
            foreach (string upload in Request.Files)
            {
                if (!Request.Files[upload].HasFile()) continue;
                string path = AppDomain.CurrentDomain.BaseDirectory + "Content/SongPhotos";
                string filename = Path.GetFileName(Request.Files[upload].FileName);
                Request.Files[upload].SaveAs(Path.Combine(path, filename));
            }

            if (ModelState.IsValid)
            {              
                db.Entry(song).State = EntityState.Modified;              
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(song);
        }
0
 
Bob LearnedCommented:
If you have a POST, with arguments, then the Request.Form values that match with the Song class should be present in the "song" property.  

If you put a breakpoint in that method, you should be able to check the Request.Form values, and see if there are any Album values.
0
 
JT_SIROAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now