ASP. NET MVC 5 builds its own AV EOG (codetemplate)

Source: Internet
Author: User
Tags actionlink

We know that when adding a view to an MVC project, there are templates to choose from in the Add panel, and there will be questions about where this template is located. Can I build my own scaffolding?

To answer the second question first, the answer is yes.

I'm using the Visual Studio 2015,asp.net MVC 5 Scaffolding template in the Catalog%ProgramFiles%\Microsoft Visual Studio 14.0\common7\ide\extensions\ Microsoft\web\mvc\scaffolding\templates

(in the case of Visual Studio 2013 or visual Studio2012, the above directory for Microsoft Visual Studio 14.0 needs to be changed to \microsoft Visual Studio 12.0)

The following shows how to build your own scaffolding

Add the following directories to the project (note: directory names must be the same.) This shows only how to build the view EOG frame, so only need to add Mvcview directory, if you need to build a controller scaffolding, you can follow the directory in the above supported directory name in the project to create a folder)

Open the Mvcview directory above and we see

There are templates for views, including C # and VB versions, because C # is used in the project, so copy the C # version to the Mvcview directory:

(Note: You need to replicate both IMPORTS.INCLUDE.T4 and MODELMETADATAFUNCTIONS.CS.INCLUDE.T4, because projects require these files to use scaffolding to generate views)

Open one of the templates and discover that the scaffolding is using the T4 template.

Because there is no code coloring and smart hints, a Visual Studio plugin Devart T4 Editor is installed to support T4 syntax highlighting, smart hints, etc.

Clicking on the download will open the download page using the browser (initially thought to download directly in Visual Studio ...), here download T4 Editor for Visual Studio 2015, download and double-click Install, here no longer describes the installation steps. Restart Visual Studio when the installation is complete.

Now open scaffolding again, find code keyword coloring

Here we try to change the return link in the Create.cs.t4 template to Chinese display

After modification:

Use the Create scaffolding when adding templates

After adding we found that the link returned is in Chinese

Here's a demonstration to build your own scaffolding.

1. Add a text template (note: The suffix TT needs to be modified to T4)

<#@ Template language="C #"Hostspecific="True"#><#@ Output extension=". cshtml"#><#@ include file="Imports.include.t4"#>@model IEnumerable<#="<"+ Viewdatatypename +">"#><#//the following chained if-statement outputs the file header code and markup for a partial view, a view using a layout page, or a regular view.if(Ispartialview) {#><#} Else if(islayoutpageselected) {#>@{Viewbag.title="<#= viewname#>";<#if(!String.IsNullOrEmpty (Layoutpagefile)) {#>Layout="<#= layoutpagefile#>";<#}#>}#} Else {#>@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title><#= ViewName #></title># pushindent ("    ");} #><p>@Html. ActionLink ("Add","Create")</p><tableclass="Table"> <tr><#IEnumerable<PropertyMetadata> Properties =modelmetadata.properties;foreach(PropertyMetadata Propertyinchproperties) {    if(property. Scaffold &&!property. IsPrimaryKey &&!Property . Isforeignkey) {#><#        //We don't want to show no association properties for which there are//no associated foreign key.        if(property. Isassociation && Getrelatedmodelmetadata (property) = =NULL) {            Continue; }#> <th>@Html. Displaynamefor (Model= model.<#= Getvalueexpression (property) #>)        </th><#    }} #> <th></th> </tr>@foreach (varIteminchModel) {    <tr><#foreach(PropertyMetadata Propertyinchproperties) {    if(property. Scaffold &&!property. IsPrimaryKey &&!Property . Isforeignkey) {#><#        //We don't want to show no association properties for which there are//no associated foreign key.        if(property. Isassociation && Getrelatedmodelmetadata (property) = =NULL) {            Continue; }#> <td>@Html. Displayfor (ModelItem= <#="Item."+ Getvalueexpression (property) #>)        </td><#    }}stringPkname =getprimarykeyname ();if(Pkname! =NULL) {#> <td>@Html. ActionLink ("Edit","Edit",New{id=item.<#= Pkname #>}) |@Html. ActionLink ("Details","Details",New{id=item.<#= Pkname #>}) |@Html. ActionLink ("Delete","Delete",New{id=item.<#= Pkname #> })        </td><#} Else {#> <td>@Html. ActionLink ("Edit","Edit",New{/*Id=item. PrimaryKey*/}) |@Html. ActionLink ("Details","Details",New{/*Id=item. PrimaryKey*/}) |@Html. ActionLink ("Delete","Delete",New{/*Id=item. PrimaryKey*/ })        </td><#}#> </tr>}</table><#//The following code closes the tag used in the case of a view using a layout page and the body and HTML tags in the CA SE of a regular view page#><#if(! Ispartialview &&!islayoutpageselected) {clearindent (); #></body>#}#><#@ include file="ModelMetadataFunctions.cs.include.t4"#>
View Code

Attach the Student.cs code

 public  class   student{ public  int  Id {get ; set     ;}  public  string  Name {get ; set     ;}  public  int  Age {get ; set     ;}  public  bool  Sex {get ; set  ;}}  
View Code

ASP. NET MVC 5 builds its own AV EOG (codetemplate)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.