Search code examples
asp.net-mvc-5asp.net-core-mvcasp.net-mvc-scaffoldingasp.net-core-mvc-2.0

Using ASP.net MVC 5 scaffolding in ASP.net Core MVC 2


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.


Solution

  • 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.