Re-Scaffold views after changing their model

12,003

Solution 1

This is a new answer to an old question. It's somewhat similar to the existing answers, but I think different enough and easy enough to be of value.

1) Save the existing project/solution to version control just as good practice.

2) When re-scaffolding, use a different controller name which will create a controller class and it's 5 attendant views, but it won't overwrite anything that exists, preserving all your existing work.

3) Extract the appropriate methods from the re-scaffolded controller. Bindings for create/edit will likely change when the model changes, so capture those. Then delete the re-scaffolded controller.

4) That leaves the views in place to copy and paste the appropriate UI code for any new or redefined model properties. Once all the code needed has been copied, simply delete the re-scaffolded views.

It was a great question because we often have to change a model, and it's nice to have all the basic UI stuff automatically created for us.

Solution 2

Yes, you can re-scaffold by scaffolding the same model again, using the same model class and controller names as before. Your existing controller and views will be replaced.

Details: Right click on your project or controller folder,

Add ... New Scaffolded Item,

MVC 5 Controller with views using Entity Framework,

Add

Choose your model and data class,

And ensure your controller name is the same as the one to replace.

Solution 3

I use version control - GIT to do it quickly and safely. I use Git Extensions (http://code.google.com/p/gitextensions/) user interface for git.

Have your code commited before re-scaffolding. Then re-scaffold the views, and go to staging (the button Commit in Git Extensions). It shows all changes that re-scaffold made and colors the new and deleted code lines. From there you can stage only the selected new lines, that changed in controller. After staging selected lines, reset the unstaged other changes.

There you have it! Your already modified code with new scaffolded parts. Do any edits and testing necessary and commit.

Share:
12,003

Related videos on Youtube

Alireza Noori
Author by

Alireza Noori

Alireza Noori - (Website) Bachelor of Science in Computer Science Full Stack developer I’m professionally familiar with: Desktop: C# (Winforms, WPF, Metro, etc.), C++, Pascal, Java and VB Web: ASP.Net (Core + RazorPages, MVC, WebAPI, etc.), PHP, JavaScript, CSS, HTML, React DB: MSSQL, SQLite, MySQL, MongoDB, ... Visit my website at: https://alirezanoori.ir/

Updated on June 04, 2022

Comments

  • Alireza Noori
    Alireza Noori almost 2 years

    I'm using Visual Studio 2013 and ASP.Net MVC 5. I've created a bunch of views for my models and then I've changed them. I want to run scaffolding on some models and create some views automatically and then change the automatically-generated views. Is there another way other than re-naming some files or creating another solution and copying stuff?

  • Alireza Noori
    Alireza Noori over 10 years
    Although this is a very nice trick, I was looking for some built-in feature or a tool specific for this purpose. I'll accept this answer if there's none
  • nu everest
    nu everest about 10 years
    Does this mean that there is nothing that lets developers Re-Scaffold their views? This happens to be one of the first things I expected to be able to do.
  • Sigex
    Sigex almost 6 years
    For anyone else @nueverest you re-scaffold by replacing. However if you are making changes to the default template then you will have to reapply these changes. I think what nueverest was hoping for was an ability to amend scaffolding rather than totally overwriting the existing. I would like this also, if anyone else has any further information please share.