0

默认视图模板的脚手架 MVC 默认生成为:

_CreateOrEdit
Edit
Create
Index

现在我想创建模板

_CreateOrEdit
Edit
Create
Index
and new template _Index as subview in Index for PageListMVC 

你能建议我怎么做吗?

4

1 回答 1

0

1.) 转到:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML

2.) 将该文件夹复制到您的解决方案中,并将其放入名为“CodeTemplates”的文件夹中

3.)删除除 List.tt 之外的所有文件(除非您正在使用它们,否则我不会在此文件夹中包含文件。完成此文件后,您可以对最初在此文件夹中的每个其他文件重复该过程。 )

4.) 在 List.tt 文件更改的属性中

Build Action: NONE
Browse To Url: BLANK
Copy to Output Dir: DO NOT COPY

5.) 将文件重命名为 List_Div,然后编辑文件以按照您的方式工作。然后当您转到脚手架时, List_Div 将是一个新选项。您必须为您想要此功能的每个项目执行此操作。但是当我开始一个新项目时,我只是将目录复制到每个项目中。我有一个专门用于 Bootstrap 项目、FOundatons 项目等的 Views 文件夹。

我会给你一个我的 Foundation List_F5 文件的例子,我只会向你展示需要更改的重要行(大约第 49 到 128 行:

    <!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title><#= mvcHost.ViewName #></title>
</head>
<body>
<#
    PushIndent("    ");
}
#>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div class="row">
    <div class="large-12 columns">
<#
List<ModelProperty> properties = GetModelProperties(mvcHost.ViewDataType);
foreach (ModelProperty property in properties) {
    if (!property.IsPrimaryKey && property.Scaffold) {
#>
        <div class="medium-1 columns">
            @Html.DisplayNameFor(model => model.<#= property.ValueExpression #>)
        </div>
<#
    }
}
#>
        <div class="medium-3 columns"></div>
    </div>

@foreach (var item in Model) {
    <div class="medium-12 columns">
<#
foreach (ModelProperty property in properties) {
    if (!property.IsPrimaryKey && property.Scaffold) {
#>
        <div class="medium-1 columns">
            @Html.DisplayFor(modelItem => <#= property.ItemValueExpression #>)
        </div>
<#
    }
}

string pkName = GetPrimaryKeyName(mvcHost.ViewDataType);
if (pkName != null) {
#>
        <div class="medium-3 columns">
            <a href="@Url.Action("Edit", "Edit", new { id=item.<#= pkName #> })"><i class="fa fa-pencil"></i></a> |
            <a href="@Url.Action("Details", "Details", new { id=item.<#= pkName #> })"><i class="fa fa-file"></i></a> |
            <a href="@Url.Action("Delete", "Delete", new { id=item.<#= pkName #> })"><i class="fa fa-times"></i></a>
        </div>
<#
} else {
#>
        <div class="medium-3 columns">
            <a href='@Url.Action("Edit", "Edit", new { /* id=item.PrimaryKey */ })"><i class="fa fa-pencil"></i></a> |
            <a href="@Url.Action("Details", "Details", new { /* id=item.PrimaryKey */ })"><i class="fa fa-file"></i></a> |
            <a href="@Url.Action("Delete", "Delete", new { /* id=item.PrimaryKey */ })"><i class="fa fa-times"></i></a>
        </div>
<#
}
#>
    </div>
}

</div>
<#
// The following code closes the asp:Content tag used in the case of a master page and the body and html tags in the case of a regular view page
#>
<#
if(mvcHost.IsContentPage) {
#>
<#
} else if(!mvcHost.IsPartialView && !mvcHost.IsContentPage) {
    ClearIndent();
#>
</body>
</html>
于 2014-07-19T08:34:04.693 回答