0

I have changed my original MVC 4 models. I dropped one object and renamed and added properties to the other.

In my Application_Start() I have the following to update my Database:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<myDB>());

public class IntranetAdsDBInitializer : DropCreateDatabaseIfModelChanges<myDB>
{
    protected override void Seed(myDB_context)
    {
        base.Seed(context);
    }
}

I re-built the project and ran it and my database now reflects the changes. Great!

But now I'm left with all the other files (Views, Controllers) that still have references to the original model. Is there a way that I can have my changes re-scaffold automatically or do I have to do everything by hand?

webdad3
  • 8,893
  • 30
  • 121
  • 223

3 Answers3

2

You still have to do it manually, but you can at least re-scaffold using the MVC-Scaffolding package, just follow these series of articles from Steve Sanderson. It will be just a one line command in the Package Manager console to re-scaffold your Views.

As an alternative, you can re-write your view to use the DisplayForModel and EditorForModel helpers, which apply to the whole model passed into the View. For example your Details View can simply be a one line:

@Html.DisplayForModel()

This means that the view will be the default Display View for a complex object and it will automatically show all properties... Keep in mind that while powerful, you are stuck with the default view (that you can override using templates however) and that it will not navigate deeper than first level properties and will have some limitation. You can read a lot about templates and their default in the splendid serie from Brad Wilson.

Tallmaris
  • 7,605
  • 3
  • 28
  • 58
1

What Tallmaris says is correct. You can also look to use the FormFactory nuget pacakage. It will automatically generate quite complex form helpers for you and they evolve with your model so need to re scaffold (which obviously loses any custom changes you have made)

It's a more advanced EditorForModel with some bells and whistles.

As the summary says: "Because you shouldn't have to update your html when you update your object model"

GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
0

Came across this recently as I had changed a model significantly and wanted to quickly update the UI code in the views. Using the Context Menu instead of the Package Manager I re-scaffolded with a different controller name so the UI code could be quickly copied and pasted into the existing body of code. See the answer this Stack Overflow post (#20556183) for specific steps.

Alan
  • 1,587
  • 3
  • 23
  • 43