3

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 ?

Jonas W
  • 3,200
  • 1
  • 31
  • 44
mina a.beigi
  • 122
  • 3
  • 13

2 Answers2

0

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

anand
  • 1,559
  • 5
  • 21
  • 45
  • this solution is choosing a .cshtml file but still the form that it has a default template replaced with `@RenderBody()` . i want to change that default aschema . for example `
    @Html.EditorFor(model => model.name) @Html.ValidationMessageFor(model => model.name)
    ` replace with my custom html like `

    @Html.EditorFor(model => model.name) @Html.ValidationMessageFor(model => model.name)
    ` for all view that i create with strongly type!
    – mina a.beigi Feb 09 '16 at 07:21
  • i want to add some tags in the scaffolding template file (`.cs.t4`) – mina a.beigi Feb 09 '16 at 07:26
  • you have to create a customize template – anand Feb 09 '16 at 07:34
  • 1
    in view folder -> Shared folder -> create a new view file according to your requirement – anand Feb 09 '16 at 07:38
  • i think you misunderstand it or my English is very bad to explain my problem :-D anyway so much thanks for your try. – mina a.beigi Feb 09 '16 at 07:58
  • did you use scaffolding before – anand Feb 09 '16 at 08:00
  • Of course! its a default template on MVC always add to my strongly type views! – mina a.beigi Feb 09 '16 at 08:07
  • then use simple process. first create a page by scaffolding and change the page as you like – anand Feb 09 '16 at 08:10
  • so i need to change for all 45 page i have! – mina a.beigi Feb 09 '16 at 08:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102968/discussion-between-anand-and-mina-a-beigi). – anand Feb 09 '16 at 08:14
0

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

ErTR
  • 863
  • 1
  • 14
  • 37