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

Posted on 2012-08-20
Last Modified: 2012-08-23

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;              
                return RedirectToAction("Index");
Question by:JT_SIRO
    LVL 96

    Expert Comment

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

    Author Comment

    Yes, it's in the postback Edit Action of my controller.  Like so:

            // POST: /Songs/Edit/5
            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;              
                    return RedirectToAction("Index");
                return View(song);
    LVL 96

    Accepted Solution

    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.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    Problem with C# 1 44
    Email Body 4 30
    Contant UI messages in .NET 1 16
    C# System Cannot Find File Specified Error 4 34
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now