Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create my own Scaffold Template in ASP.NET MVC 3?

ASP.NET MVC provides the ability to select a 'Scaffold template' upon which a newly-created view will be based (Add View > Create a strongly-typed view > Scaffold template).

Is it possible to create your own Scaffold Template? And if so, how?

like image 996
Ryan Shripat Avatar asked Jan 14 '11 14:01

Ryan Shripat


People also ask

How can you create a scaffold template in MVC?

To add a scaffold, right-click on Controllers folder in the Solution Explorer and select Add → New Scaffolded Item. It will display the Add Scaffold dialog. Select MVC 5 Controller with views, using Entity Framework in the middle pane and click 'Add' button, which will display the Add Controller dialog.

What are scaffold templates in ASP.NET MVC?

Scaffold templates are used to generate code for basic CRUD operations within your ASP.NET MVC applications against your database with the help Entity Framework. These templates use the Visual Studio T4 templating system to generate views for basic CRUD operations with the help of Entity Framework.

How do you create scaffolding in .NET core?

From Solution Explorer, right-click on the project > Add > New Scaffolded Item. From the left pane of the Add Scaffold dialog, select Identity > Add. In the Add Identity dialog, select the options you want. Select your existing layout page so your layout file isn't overwritten with incorrect markup.


2 Answers

ASP.NET MVC uses T4 templates. Here's an overview.

Here are the steps:

  1. In the Package Manager Console type: install-package mvc3codetemplatescsharp
  2. Accept all the warnings
  3. The CodeTemplates folder will be added to your project containing the templates

From here you could either modify the existing templates or add new one.

Or if you want to modify those globally you could to this in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\ folder.

like image 167
Darin Dimitrov Avatar answered Sep 27 '22 20:09

Darin Dimitrov


You can use T4 without nuget of course: Place a folder in the root of the application website (the project containing the views). The directory structure is important so it should be

\CodeTemplates\AddView\AspxCsharp\MyTemplate.tt

You can copy the contents from one of the existing templates located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\

Next, clear the property on the TT file named "Custom Tool". This should be blank.

Then right-click on any Controller Action and say "Add View" or since the controllers are in a separate project in our case, right click on the View folder and click "Add View".

From the dropdown Click "Create a strongly typed View" and then enter the type to use under "View Data Class:"

Finally, in the "View Content" dropdown, select "MyTempate". This should show up if you've entered the folders correctly.

like image 33
Francis Shanahan Avatar answered Sep 27 '22 21:09

Francis Shanahan