Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaffolding and Display/EditorTemplates

Say I have the following models:

public class Item
{
    public int Id{ get; set; }
    public ItemDescription ItemDescription{ get; set; }
}
public class ItemDescription
{
    public int Id{ get; set; }
    public int Revision{ get; set; }
    public string Test{ get; set; }
}

I run the following in the package-manager:

Scaffold Controller Item

to generate some views and stuff... but it appears that the scaffolder ignores any complex/non-scalar data types and consequently generates views that are of little use.

I am wondering if it is possible to instruct the scaffolder to be a little more intelligent about things. Here's what I would like to happen:

  • scaffolder creates Editor/Display templates in the shared folder
  • uses EditorFor to leverage these templates

All the code to make this happen seems to be generated by the scaffolder, but is structured in a way that surprises me, with _CreateOrEdit.cshtml "templates" generated in the folder associated with the view. To me, this suggests that the scaffolder generates code that would not be ideally suited to a more recursive way of generating views for models.

Are my expectations way off, or am I missing something?

like image 477
spender Avatar asked Oct 09 '22 08:10

spender


1 Answers

Description

You can find the T4 Templates that Visual Studio uses to generate the Code in

YourPathToVisualStudio2010\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates

change the templates you need to solve your problem in general. Another thing you can do is to create a template and add them to your particular project / solution.

Check out the great Scott Hanselman - T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret post.

I really encourage you to read the links in my "more information" section.

Have a nice day!

Update

Since a few days the whole (not only mvc) microsoft webstack (of love, thx scott hanselman for this term ;)) is open source on codeplex ASP.NET Webstack. Check it out!

MVC4 is not in RTM Version, but maybe you are interested in.

Recipes for Code Generation in Visual Studio (MVC4)

The new Recipes feature enables Visual Studio to generate solution-specific code based on packages that you can install using NuGet. The Recipes framework makes it easy for developers to write code-generation plugins, which you can also use to replace the built-in code generators for Add Area, Add Controller, and Add View. Because recipes are deployed as NuGet packages, they can easily be checked into source control and shared with all developers on the project automatically. They are also available on a per-solution basis.

Feel free to ask additional questions. Please use the comment feature of my answer.

More Information

  • MSDN - Code Generation and T4 Text Templates
  • Scott Hanselman - T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
  • video mvcConf 2 - Steve Sanderson: MvcScaffolding
  • MvcScaffolding: Overriding the T4 Templates
  • MvcScaffolding: Creating custom scaffolders
  • ASP.NET Webstack
like image 154
dknaack Avatar answered Oct 13 '22 09:10

dknaack