ASP.NET MVC 5搭建自己的視圖基架 (CodeTemplate)

來源:互聯網
上載者:User

標籤:目錄   action   details   isp   sts   isnull   複製   class   studio   

 

 

我們知道,在MVC項目中添加視圖時,在添加面板有模板可以選擇,這裡會有人疑問,這個模板位於哪裡?我可以搭建自己的基架嗎?

首先回答第二個問題,答案是當然可以

我這裡使用的是Visual Studio 2015,ASP.NET MVC 5的基架模板位於目錄%programfiles%\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates

(如果是Visual Studio 2013或者Visual Studio2012,上述目錄Microsoft Visual Studio 14.0需要改成\Microsoft Visual Studio 12.0)

下面示範如何搭建自己的基架

項目中添加如下的目錄(注意:目錄名稱必須一致。這裡只示範如何搭建視圖基架,所以只需要添加MvcView目錄,如果需要搭建控制器基架,可以按照上述目錄中所支援的目錄名在項目中去建立一個檔案夾)

 

 

開啟上述的MvcView目錄,我們看到

這裡有視圖的模板,包含C#版和VB版,由於項目中使用的是C#,所以拷貝C#版的到MvcView目錄下:

 

(注意:需要同時複製Imports.include.t4和ModelMetadataFunctions.cs.include.t4,這是因為項目需要這些檔案才能使用基架產生視圖)

開啟其中一個模板,發現基架使用的是T4模板。

由於沒有代碼著色與智能提示,這裡安裝了一個Visual Studio外掛程式Devart T4 Editor,用於支援T4文法高亮,智能提示等

點擊下載會使用瀏覽器開啟下載頁面(起初還以為直接在Visual Studio中直接下載...),這裡下載T4 Editor for Visual Studio 2015,下載後雙擊安裝,這裡不再敘述安裝的步驟。安裝完成後重啟一下Visual Studio。

 

 

此時再次開啟基架,發現代碼關鍵詞著色

這裡我們嘗試將Create.cs.t4模板中的返回連結修改為中文顯示

 

修改後:

添加模板時使用Create基架

添加後我們發現,返回的連結是中文

下面示範搭建自己的基架

1. 添加文本模板(注意:需要將尾碼tt修改成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#>";<#}#>}<h2>資料列表頁面</h2><#} else {#>@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title><#= ViewName #></title></head><body><#    PushIndent("    ");}#><p>    @Html.ActionLink("添加", "Create")</p><table class="table">    <tr><#IEnumerable<PropertyMetadata> properties = ModelMetadata.Properties;foreach (PropertyMetadata property in properties) {    if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey) {#><#        // We do not want to show any association properties for which there is        // no associated foreign key.        if (property.IsAssociation && GetRelatedModelMetadata(property) == null) {            continue;        }#>        <th>            @Html.DisplayNameFor(model => model.<#= GetValueExpression(property) #>)        </th><#    }}#>        <th></th>    </tr>@foreach (var item in Model) {    <tr><#foreach (PropertyMetadata property in properties) {    if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey) {#><#        // We do not want to show any association properties for which there is        // no associated foreign key.        if (property.IsAssociation && GetRelatedModelMetadata(property) == null) {            continue;        }#>        <td>            @Html.DisplayFor(modelItem => <#= "item." + GetValueExpression(property) #>)        </td><#    }}string pkName = GetPrimaryKeyName();if (pkName != null) {#>        <td>            @Html.ActionLink("編輯", "Edit", new { id=item.<#= pkName #> }) |            @Html.ActionLink("詳情", "Details", new { id=item.<#= pkName #> }) |            @Html.ActionLink("刪除", "Delete", new { id=item.<#= pkName #> })        </td><#} else {#>        <td>            @Html.ActionLink("編輯", "Edit", new { /* id=item.PrimaryKey */ }) |            @Html.ActionLink("詳情", "Details", new { /* id=item.PrimaryKey */ }) |            @Html.ActionLink("刪除", "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 case of a regular view page#><#if(!IsPartialView && !IsLayoutPageSelected) {    ClearIndent();#></body></html><#}#><#@ include file="ModelMetadataFunctions.cs.include.t4" #>
View Code

 

 附上Student.cs的代碼

 

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搭建自己的視圖基架 (CodeTemplate)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.