Re-Scaffold views after changing their model

强颜欢笑 提交于 2020-04-07 18:41:06

问题


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?


回答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.




回答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.




回答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.



来源:https://stackoverflow.com/questions/20556183/re-scaffold-views-after-changing-their-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!