how customizing scaffolding template and change .cs.t4 file in MVC

1.1k Views Asked by At

in MVC when we need a form like below that create a new item of my model, we add a strongly type view on the model with create scaffold template, model:

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; }
} ` 

the result is something like this:

    @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")
} `

this template follow this path:

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

what i need is a knowledge to how change this file and in fact what is this file and what part change the template ?

2

There are 2 best solutions below

10
anand On

First create a custom layout(template) in the Shared Folder in view according to the page requirement.
Then on scaffolding, choose the layout (customized template), model etc and click ok. Thats all

0
ErTR On

Weirdly, it was so hard to find...

Global edit:

You can modify the original templates, which will globally affect scaffolding across all Visual Studio projects. The original T4 scaffold templates are located in the %programfiles%\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates folder.

Project-specific edit:

To create project-specific templates, copy the files you want to override from the original T4 scaffold folder to a folder in the ASP.NET MVC Web project called CodeTemplates (it must have this exact name). By convention, the scaffold subsystem first looks in the MVC project’s CodeTemplates folder for a template match.

For this to work, you must precisely replicate the specific sub-folder names and file names you see in the original templates folder.

Further read: https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/june/asp-net-mvc-override-the-default-scaffold-templates