0

In MVC 5, when you scaffold a View, Create, with a model - in order to build a form that will be bound to the model - the scaffolder in MVC 5 used columns (col-md-10 and col-md-2) to put labels to the left of textboxes:

<div class="form-group">
    @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
        @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
    </div>
</div>

In Core MVC 2, all labels are above text boxes - which is super space inefficient:

<div class="form-group">
    <label asp-for="Email" class="control-label"></label>
    <input asp-for="Email" class="form-control" />
    <span asp-validation-for="Email" class="text-danger"></span>
</div>

I could edit all of this by hand but this defeats the object of scaffolding and will take a long time.

Is there any way to get the scaffolder to use the previous col type of layout - or are there any third party options?

I can find nothing in Google. Thanks.

2
  • 1
    Well, you can always create your own scaffolding using T4 templates, but man if that isn't the most obtuse waste of time ever. It takes literally no time to build out a form like this by hand. Scaffolding is not intended to write all your code for you; it's expected that you'll be changing it. Commented Apr 19, 2018 at 14:20
  • If you're building a lot of forms (I am) doing a repetitive change again and again is something to be avoided (DRY) - in MVC 5 it made a lot more sense. Who wants to have labels above text boxes?! Gotta love the unexplained drive by down vote. Commented Apr 19, 2018 at 16:29

1 Answer 1

1

The scaffolding templates are pretty easy to edit to your tastes.

In Asp.Net Core MVC 2 the templates are now cshtml rather than t4.

You simply copy the templates into your project as explained here, then the next time you run the scaffolder it'll use your templates rather than the default templates.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks though the path they list doesn't exist (packages folder doesn't contain Microsoft.VisualStudio...... folder). Has this been changed?
I'm using VS2017 and mine are here: C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.visualstudio.web.codegenerators.mvc\2.0.3\Templates
If you can't find them try searching for MvcControllerWithContext.cshtml - that's a file in one of the template folders, you should then be able to find the parent Templates directory from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.