joeriks
joeriks

Reputation: 3462

Default generic views from models in Asp Net Mvc

In Asp Net Mvc (3) I like to be able to have a default set of views that's made with some kind of generic viewbuilder (+formbuilder) - so I wont need to create them on my own for each new controller (and for each model change). This is for many cases where the views are just has the same simple structure. For specialized views I would ofcourse go for the manual approach.

I thought of two options.

One is file-less views, where all the necessary html is generated directly from the controller (if the view-file for the actual view alternative is missing).

The other is shared views for each view alternative, that are setup with the help of route rules.

In the shared view file for edit (for example) the code could look something like this, building the form iterating over the model properties : Validating dynamically created fields in ASP.NET MVC

I think this is something that should come out of the box in Mvc, and perhaps I am missing something? Otherwise I'll give it a go making something myself. Would be happy to know how others think (and do) about this.

Edit : yes, duh, :-), most important part of it comes out of the box indeed, I had missed that - just by adding a set of default views in /Views/Shared folder that one is activated if the controller-specific view is missing. Just like the error message is sayin:

The view 'Edit' or its master was not found. The following locations were searched: ~/Views/UserSubCategorySimplified/Edit.cshtml ~/Views/Shared/Edit.cshtml

(Yes scaffolding is great and a compromise - but after a new change in the model one has to re-make all files.)

Edit : a "recreate all views for this controller"-function would also be good. (After a model change or T4-change).

Thanks!

Jonas

Upvotes: 0

Views: 933

Answers (1)

marcind
marcind

Reputation: 53183

Have you looked at DisplayForModel and EditorForModel methods? More info here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

Upvotes: 1

Related Questions