?
Solved

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

Posted on 2012-08-20
4
Medium Priority
?
1,298 Views
Last Modified: 2012-08-23
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
Comment
Question by:JT_SIRO
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 38320524
Is there a post back that calls the db.SaveChanges?  Is that code in a controller action method?
0
 

Author Comment

by:JT_SIRO
ID: 38322948
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1400 total points
ID: 38323231
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
 

Author Closing Comment

by:JT_SIRO
ID: 38326140
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question