我们知道,在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" #>

附上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; }
}

ASP.NET MVC 5搭建自己的视图基架 (CodeTemplate)的更多相关文章

  1. Unity + iBatis + Asp.net Mvc 系统搭建

    Unity + iBatis + Asp.net Mvc 系统搭建 之前用EntityFramework Code First做了一些小项目,很是方便:后来在一个 Java 项目中接触了myBatis ...

  2. Easyui入门视频教程 第02集--- ASP.NET MVC下 搭建 EasyUI环境

    Easyui入门视频教程 第02集--- ASP.NET MVC下 搭建 EasyUI环境 目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标 ...

  3. ASP.NET MVC+Bootstrap个人博客之praise.js点赞特效插件(二)

    1. 为啥要做这个点赞插件?    praise.js是一款小巧的jQuery点赞插件,使用简便,效果美观. 在做个人博客时遇到了文章点赞问题.联想到各大社交网络中的点赞特效:手势放大.红心放大等等, ...

  4. Asp.net MVC 插件式应用框架

    Asp.net MVC 插件式应用框架 2013年05月13日 10:16供稿中心: 互联网运营部 摘要:这几年来做了很多个网站系统,一直坚持使用asp.net mvc建站,每次都从头开始做Layou ...

  5. ASP.NET MVC实现通用设置

    网站中的设置实现方式有好几种,其中有将设置类序列化然后保存到文件中(例如使用XML序列化然后以XML形式保存在文件中),或者将设置信息保存到数据库中. 保存到数据库中的方式就是将设置的项作为key,设 ...

  6. ASP.NET+MVC入门踩坑笔记 (一) 创建项目 项目配置运行 以及简单的Api搭建

    哈喽各位 我又回来了! 前段时间研究了下ASP.NET,刚开始也是随便找网上的各种教程来看,但是鉴于本人技术有限,还是走了相当长的一段弯路的.所以我写下了这篇文章.希望各位刚刚入坑的ASP.NET开发 ...

  7. ASP.NET MVC搭建项目后台UI框架—1、后台主框架

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  8. ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询

    ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...

  9. Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)

    1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...

随机推荐

  1. IntelliJ IDEA的黑白色背景切换(Ultimate和Community版本皆通用)

    不多说,直接上干货! IntelliJ IDEA的黑白色背景切换 File    ->   Setting    ->  Editor     ->   Colors & F ...

  2. Disconf 学习系列之Disconf是什么?

    不多说,直接上干货! Disconf是什么 Distributed Configuration Management Platform(分布式配置管理平台) ,它是专注于各种分布式系统配置管理 的通用 ...

  3. MyEclipse *的下载

    找到MyEclipse的各种历史版本下载页面 : MyEclipse官方中文网 欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智能躺过的坑       同时,大家可以关注我的个人博客 ...

  4. Maven Debug

    1.在pom.xml中新增plugin <plugin>   <groupId>org.mortbay.jetty</groupId>   <artifact ...

  5. [CQOI 2018]九连环

    Description 题库链接 给你一个 \(n\) 连环,游戏规则是: 第一个(最右边)环任何时候都可以任意装上或卸下: 如果第 \(k\) 个环没有被卸下,且第 \(k\) 个环右边的所有环都被 ...

  6. Git报错:error: cannot open .git/FETCH_HEAD: Read-only file system

    Git:git pull时报错 error: cannot open .git/FETCH_HEAD: Read-only file system 查看该文件: 未在网上找到解决办法,重启服务器就好了 ...

  7. bind9配置转发服务

    修改bind主配置文件 $ vi /etc/named.conf//// named.conf//// Provided by Red Hat bind package to configure th ...

  8. sqlserver中调用服务器中的webservice接口

    declare @ServiceUrl as varchar(1000) declare @UrlAddress varchar(500)--WebService地址:以http开头,结尾带斜杠,例如 ...

  9. @EnableAutoConfiguration和@SpringbootApplication注解

    一.@EnableAutoConfiguration 这个注释告诉SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项.如果spring-boot-starter-web已 ...

  10. 微信小程序开发总结(详细)

    转载: 小程序开发总结(详细) 这段时间一直在做小程序,总结下.后续可能会不断更新,努力写仔细点,争取让人看到就能会写. 一,页面结构. 这基本是小程序的标准目录结构.我们从上到下解释下:pages文 ...