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?
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.
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.
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.
ASP.NET MVC uses T4 templates. Here's an overview.
Here are the steps:
install-package mvc3codetemplatescsharp
CodeTemplates
folder will be added to your project containing the templatesFrom 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With