Updating scaffolded controllers and views with model changes - asp.net-mvc

For the basic functionality of my project I've been defining Entity Framework models and then using the scaffolding feature of Visual Studio to implement CRUD functionality (Right Click -> Add -> New Scaffolded Item...).
If I need to make a change to the model after I've done this, how do I make the scaffolded controllers and views update to the latest model changes? For example, I'm seeing the need now to add a new attribute to my model and change the data type of an existing one, but the controllers and views likely won't work after the change.
I could always just re-scaffold, but I've made some changes to the controller and views already which I don't want to lose. Is my only solution to make the changes myself?

Rename the files having your changes, scaffold again, and replace the newly-scaffolded files with your originals.
Naturally, you'll have to reconcile any domain changes that were made to the files containing your changes.

Related

Is there a way to update/re-generate .cshtml files on Visual Studio (the ones generated when adding controller)?

I updated my .edmx model file and now I want the generated edit.cshtml file and create.cshtml files updated according to it. Is there a way to get it done automatically?
What i'd do is:
Backup related data from controllers and views.
Delete Views and Controllers that you need to generate
Create controllers with use of same scaffolding you used before.
Generate views for controllers using that scaffolding
Use your backup to update all that controller/views with logic that you had before (or skip this step if you don't need that).

Need an Automatic Editor

what I'm looking for is some something similar to the automatically generated "delete" and "Create" functions that are created in MVC. I'd like one that is used to update a record. Preferably it would work much like a gridview (I'd prefer not to mix in ASPX pages), but if it creates a separate form, I'm okay with that too.
The best way to get started is to scaffold a controller. In Visual Studio right-click the Controller folder, Add > Controller, you'll have the option to scaffold a controller with Read/Write actions or using Entity Framework. In the latter case you'll need a create model to base it on before hand.
Once you've done this have a look for the Edit ActionResult, this allows you to edit single records. It's possible to do multiple records but it take a fair bit more work.

What are pros and cons of adding a Model to MVC using EF VS project before and after a Controller and a View?

In tutorials and walkthroughs on developing an MVC3 (MVC2) using EF (EntityFramework, Entity Framework 4.1/4.2) I observe quite different orders of adding Model, View, Controller to a project in a Microsoft Visual Studio 2010.
What are cons and pros of different orders of adding M, V and Cs?
and of, for example, adding a model before and, more specifically, after a view and controller?
There is no specific rules for adding one over the other first. When you create an empty ASP.NET MVC3 Project, It will come with some default folder structure which includes one Controller folder, Views folder and Models folder.
Now If you are beginner, This is what i suggest. Add a controller first.
Simply right click ont he Controller folder and Select Add->Controller from the context menu and add your first controller(Give the name as HomeController). It will come with a Default Index action method and you can see a return View statement. Run your project now. It will show you an error saying that it can not find a view. So now it is the time to add a view. Go the index action in home controller. Right click on the Return View() statement and select Add View that will add a view (index.xshtml) under the home folder under Views. Now run the app and you will see the page content.
If you are going to interact with your database, you can add model classes. If you can add POCO class file to your Models folder or you can have it on a different library which is refered to this projcet. It's all up to you.
As Lavinski mentioned, If you create your models first, You can use Scaffolding to create the controller actions for you. But If you are beginner, I would suggest you to create your controllers and views by hand. That will help you to understand MVC configuration works

mvc rebuild strongly typed view

is there any way to re-build a strongly typed view when the model class has new fields added?
At present I have not modified the initial generated view so deleting and re-creating is not a problem.
When I start to customize it to my liking I will lose all changes and I was wondering if there was a good way to manage this?
Thanks
ASP.NET MVC offers two types of scaffolding, each with its own advantages:
The first kind of scaffolding is design-time scaffolding, which is done through the Add View dialog and T4 templates. The advantage of this is that the code is entirely generated and you can completely customize it. The disadvantage is that if you change your model you have to regenerate your view (by deleting it and adding back a new one).
The second kind of scaffolding is runtime scaffolding, which is done through the Html.EditorFor() and Html.DisplayFor() methods in your view. The advantage of this is that if your model changes then the scaffolding will be automatically generated at runtime. The disadvantage is that you cannot directly customize the rendering. You can, however, give this scaffolding many hints using DataAnnotations attributes such as [DisplayText], [UIHint], and so forth, so it is quite flexible - but it is not nearly as flexible as being able to 100% customize the rendering.
To customize the rendering of runtime scaffolding (editor templates and display templates) you can find more info on Brad Wilson's blog series.
You can manually add code to your view to reflect any changes in the model.
Default scafolding is there just to give you something.
One thing you might want to look at is t4 templates using wich mvc tooling generates your default views. google for it there are examples of how you can copy those to your project and modify to have mvc generate views you want instead of default ones.

Add a field to a Grails Domain Class?

I would like to add a field to an existing domain class. I do not want to lose my view files however as I know will happen if i run generate-all. Is there another way I can update the mapping etc?
I think this is a common concern.
This is not a direct solution to your problem, but this is what I do and works very well for me.
I never make direct modifications to the grails scaffolded artifacts (views and controllers)
I keep my production views/controllers separate from the scaffolded artifacts; through I use the scaffolded as the starting point of my application controllers and views.
If there are changes to the domain model, I re-generate the views and copy-paste (wherever possible) from scaffolded artifacts to hand-coded artifacts.
At some point, I either delete all the scaffolded artifacts from the app or just protect access to them.
Hope this helps.
There are a couple of ways to do this.
If you have not modified your generated views too much and are using version control, you could allow grails to overwrite your views and then merge the changes to the new templates with what is in version control.
If you have modified your generated views, you could just answer no to the prompt for overwriting the views. If you are only adding a new field, changing a field name or something simple like that, modifying the view templates should be pretty straight forward to do manually.
You would deal with any possible changes to the Controllers in the same way.
just declare it:
class MyDomain {
String newField;
}
Should be all you need to do.
Don't regenerate the views, just copy and paste another similar field to the one you are putting in the view(s) you want.

Resources