Need an Automatic Editor - asp.net-mvc

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.

Related

Updating scaffolded controllers and views with model changes

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.

ASP.NET MVC EditorTemplate sub folders

I am working on, what I would consider to be, a large ASP.NET MVC website. Currently there are nearly 100 editor templates (all for 1 controller), and this number will grow.
What I want to accomplish is organize my views to make them easier to find and version. This 'version' step is what will make the views multiply as time goes on. you can think of this project as an Question/Answer application, where Exams are created, and can be pulled up later. Basically, for this particular project, the views/EditorTemplates can't really change once in production, so a new copy must be created for future use. References to the old view would still exist, making that exam look and behave the way it did a year ago. Likewise, new exams will automatically pick up the new version of the view, and use that version.
I would like to have this type of structure, but I am up for other ideas.
Views/Shared/EditorTemplates/Common
Views/Shared/EditorTemplates/Common/v2
Views/Shared/EditorTemplates/Common/v3
Views/Shared/EditorTemplates/Department
Views/Shared/EditorTemplates/Department/v2
Note: Even though I will have versioned subdirectories, which implies that I will have multiple versions of the same model and template, the new files will have a unique file name. Also, I am also attempting to use the Razor Generator to compile my views. Not sure if that can be extended to add the additional EditorTemplate search paths or not.
The framework won't look there, use local EditorTemplate folders instead, e.g. Views/Department/EditorTemplates.
Editor templates are located by the view engine, which first looks in ~/Views/{1}/{0}.cshtml and then in ~/Views/Shared/{0}.cshtml.
For example, if the controller is Department and the model is a String, the framework asks for EditorTemplates/String, and the view engine looks in ~/Views/Department/EditorTemplates/String.cshtml and ~/Views/Shared/EditorTemplates/String.cshtml.
Max's answer is a much more elegant and simple answer. If you don't want to do that AND you want a ton of work, you can write your own ViewEngine.

Can I create my own scaffolding resource i.e. add presentation model purposes 'controller'

Due to using Grails app also as open-dolphin server, I'd like to be able to scaffold presentation model from domain classes. Since I need to put them into shared package to include them also to client, putting them in standard controller scaffold source is not suitable for me.
So, the question is: Is it possible to add extra scaffolding source beside controller, edit, create, show, etc?
Of course you can, just do "grails install-templates" and add new actions to the scaffolded controller: http://grails.org/doc/latest/guide/scaffolding.html

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.

Resources