c# - 如何在 MVC 中自定义脚手架模板和更改 .cs.t4 文件

标签 c# asp.net-mvc asp.net-mvc-4 t4scaffolding

在 MVC 中,当我们需要一个像下面这样的表单来创建我的模型的一个新项目时,我们在模型上添加一个强类型 View ,创建脚手架模板, 型号:

public class book
{
    [Key]
    public int BId { get; set; }
    [Display(Name = "نام")]
    public string name { get; set; }
    [Display(Name = "نویسنده")]
    public string writer { get; set; }
    [Display(Name = "ناشر")]
    public string publisher { get; set; }
    [Display(Name = "سال انتشار")]
    public string year { get; set; }
} ` 

结果是这样的:

    @model مدرسه.Models.book
`@{
    ViewBag.Title = "BookStore";
} `
` <h2>BookStore</h2>`
    @using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true) 
    <fieldset>
        <legend>book</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.writer)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.writer)
            @Html.ValidationMessageFor(model => model.writer)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.publisher)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.publisher)
            @Html.ValidationMessageFor(model => model.publisher)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.year)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.year)
            @Html.ValidationMessageFor(model => model.year)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
`}`

    <div>
    @Html.ActionLink("Back to List", "Index")
</div> 

 `@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
} `

此模板遵循此路径:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding

我需要的是了解如何更改此文件以及实际上此文件是什么以及更改模板的哪一部分?

最佳答案

首先根据页面需求在 View 的Shared Folder中创建一个自定义布局(模板)。
然后在脚手架上,选择布局(自定义模板)、模型等,然后单击确定。就这些

关于c# - 如何在 MVC 中自定义脚手架模板和更改 .cs.t4 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35284652/

相关文章:

c# - 在数据库中存储大文件会导致类型为 'System.OutOfMemoryException' 的异常

javascript - 如何从外部js文件方法调用内部脚本函数?

c# - 更改 For 辅助方法创建的名称

c# - 如何在 XSD 中为 byte[] 类型建模?

c# - WP 8.1 XAML 应用程序在 MapControl 的 Windows 10 Mobile 上时常崩溃

c# - OData 新实体但部分有效负载

sql - 使用 Entity Framework 4.1 的多表连接,我应该使用 lambda 还是 LINQ?

c# - 将文本添加到单选按钮

c# - 单个 View 中的多个模型 : submit failure

c# - 结果集存储在模型上 - C# .NET 2.0