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.

niico
  • 11,206
  • 23
  • 78
  • 161
  • 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. – Chris Pratt Apr 19 '18 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. – niico Apr 19 '18 at 16:29

1 Answers1

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.

jmdon
  • 997
  • 9
  • 17
  • Thanks though the path they list doesn't exist (packages folder doesn't contain Microsoft.VisualStudio...... folder). Has this been changed? – niico Apr 26 '18 at 13:13
  • I'm using VS2017 and mine are here: C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.visualstudio.web.codegenerators.mvc\2.0.3\Templates – jmdon Apr 27 '18 at 09:54
  • 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. – jmdon Apr 27 '18 at 09:55