|
| 1 | +--- |
| 2 | +title: Scaffolding |
| 3 | +page_title: Scaffolding | Progress Telerik UI for ASP.NET Core |
| 4 | +description: "Learn how to use the Kendo UI Scaffolder extensions." |
| 5 | +slug: scaffolding_aspnetcore |
| 6 | +position: 4 |
| 7 | +--- |
| 8 | + |
| 9 | +# Scaffolding |
| 10 | + |
| 11 | +As of the R1 2019 release, Telerik UI for ASP.NET Core provides Scaffolding templates. |
| 12 | + |
| 13 | +They allow you to use the standard scaffolding to generate MVC-helper declarations for editors that use the Telerik UI for ASP.NET Core components instead of standard inputs. |
| 14 | + |
| 15 | +You can use them in both MVC views and Razor Pages. |
| 16 | + |
| 17 | +Depending on the type of field, the following editors will be generated: |
| 18 | + |
| 19 | +* `String`: `input` element with Kendo styles |
| 20 | +* `String` (multiline): `textarea` element with Kendo styles |
| 21 | +* `Number`: `kendo-numerictextbox` tag helper |
| 22 | +* `DateTime`: `kendo-datetimepicker` tag helper |
| 23 | +* `Boolean`: `Html.Kendo().CheckBoxFor` helper |
| 24 | + |
| 25 | +The scaffolding logic and tools are the same as with the ordinary .NET Core web applications. The main repository and logic for the scaffolding provided by Microsoft is [https://github.com/aspnet/Scaffolding](https://github.com/aspnet/Scaffolding). The original template files are located in its: [https://github.com/aspnet/Scaffolding/tree/master/src/VS.Web.CG.Mvc/Templates](https://github.com/aspnet/Scaffolding/tree/master/src/VS.Web.CG.Mvc/Templates) folder. |
| 26 | + |
| 27 | +In order to use the scaffolding provided by Microsoft you can follow these resources: |
| 28 | + |
| 29 | +- [Add a model to an ASP.NET Core MVC app](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.1) |
| 30 | +- [Add a model to a Razor Pages app in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-2.1) |
| 31 | + |
| 32 | + |
| 33 | +>tip The Scaffolding features allow you to generate templates based on classes and are different than the editor templates used by widgets like the grid for editing records. If you are looking for the editor templates, you can find them in your installation folder under `C:\Program Files (x86)\Progress\Telerik UI for ASP.NET Core <your version>\wrappers\aspnetcore\EditorTemplates`. |
| 34 | +
|
| 35 | +## Getting Started |
| 36 | + |
| 37 | + |
| 38 | +### Requirements |
| 39 | + |
| 40 | +Make sure that Telerik UI for ASP.NET Core is installed and set up in your project. There are two ways to do that: |
| 41 | + |
| 42 | + * [Use Visual Studio](https://docs.telerik.com/aspnet-core/getting-started/getting-started) |
| 43 | + * [Use the CLI](https://docs.telerik.com/aspnet-core/getting-started/getting-started-cli) |
| 44 | + |
| 45 | +### Installation |
| 46 | + |
| 47 | +To install the UI for ASP.NET Core Scaffolding templates and extensions in your project: |
| 48 | + |
| 49 | +1. Clone or Download the [https://github.com/telerik/scaffold-templates-core](https://github.com/telerik/scaffold-templates-core) repository. |
| 50 | + |
| 51 | +1. Copy the `Templates` folder from the repo above to the **root** of your project. |
| 52 | + |
| 53 | +1. Exclude it from the project to make sure it will not be compiled when building. There are two ways to do that: |
| 54 | + |
| 55 | + * add the following to the project file directly: |
| 56 | + |
| 57 | + ```.csproj |
| 58 | + <ItemGroup> |
| 59 | + <Compile Remove="Templates\**" /> |
| 60 | + <Content Remove="Templates\**" /> |
| 61 | + <EmbeddedResource Remove="Templates\**" /> |
| 62 | + <None Remove="Templates\**" /> |
| 63 | + </ItemGroup> |
| 64 | + ``` |
| 65 | + |
| 66 | + * OR, use the GUI in Visual Studio: |
| 67 | + |
| 68 | + **Figure 1. Exclude the Templates folder from compilation** |
| 69 | + |
| 70 | +  |
| 71 | + |
| 72 | + |
| 73 | +### Use With Visual Studio |
| 74 | + |
| 75 | +The Kendo UI Scaffolding Templates pertain to cshtml generation, so you must first create the appropriate model and controller as per your applicaiton needs. You can use the standard Scaffolding, or code you already have. |
| 76 | + |
| 77 | +If you don't have a model and controller already, below the list you can find a sample set so you can follow this example. |
| 78 | + |
| 79 | +To add a View that will use a Kendo scaffolded template: |
| 80 | + |
| 81 | +1. Select **Add** > **New Scaffolded Item...** from the context menu of the desired location. |
| 82 | + |
| 83 | + **Figure 2. Add Scaffolded Item** |
| 84 | + |
| 85 | +  |
| 86 | + |
| 87 | +1. Choose item type (for example, **MVC View**) and click **Add** |
| 88 | + |
| 89 | + **Figure 3. Add Scaffolded View** |
| 90 | + |
| 91 | +  |
| 92 | + |
| 93 | +1. Set the View properties (name, template and model to match the code you already have) and click **Add**. For example, an `Edit` view for the `SampleScaffoldingData` model. |
| 94 | + |
| 95 | + **Figure 3. Set View Properties** |
| 96 | + |
| 97 | +  |
| 98 | + |
| 99 | +After following the steps, you should have something similar to the following in your project. You can also use the sample class and controller to test the behavior with dummy data instead of actual models. |
| 100 | + |
| 101 | +```Model |
| 102 | +using System; |
| 103 | +using System.Collections.Generic; |
| 104 | +using System.ComponentModel.DataAnnotations; |
| 105 | +using System.Linq; |
| 106 | +using System.Threading.Tasks; |
| 107 | + |
| 108 | +namespace SampleCoreApp.Models |
| 109 | +{ |
| 110 | + public class SampleScaffoldingData |
| 111 | + { |
| 112 | + public string SingleLineString { get; set; } |
| 113 | + [DataType(DataType.MultilineText)] |
| 114 | + public string MultiLineString { get; set; } |
| 115 | + public int SomeNumber { get; set; } |
| 116 | + public DateTime SomeDate { get; set; } |
| 117 | + public bool SomeBoolean { get; set; } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | +```View |
| 122 | +@model SampleCoreApp.Models.SampleScaffoldingData |
| 123 | +
|
| 124 | +@{ |
| 125 | + ViewData["Title"] = "Edit"; |
| 126 | +} |
| 127 | +
|
| 128 | +<h1>Edit</h1> |
| 129 | +
|
| 130 | +<h4>SampleScaffoldingData</h4> |
| 131 | +<hr /> |
| 132 | +<div class="row"> |
| 133 | + <div class="col-md-4"> |
| 134 | + <form asp-action="Edit" id="SampleScaffoldingDataForm"> |
| 135 | + <div asp-validation-summary="ModelOnly" class="text-danger"></div> |
| 136 | + <div class="form-group"> |
| 137 | + <label asp-for="SingleLineString"></label> |
| 138 | + <input asp-for="SingleLineString" class="k-textbox" /> |
| 139 | + <span asp-validation-for="SingleLineString" class="text-danger k-invalid-msg" data-for="SingleLineString"></span> |
| 140 | + </div> |
| 141 | + <div class="form-group"> |
| 142 | + <label asp-for="MultiLineString"></label> |
| 143 | + <textarea asp-for="MultiLineString" class="k-textbox"></textarea> |
| 144 | + <span asp-validation-for="MultiLineString" class="text-danger k-invalid-msg" data-for="MultiLineString"></span> |
| 145 | + </div> |
| 146 | + <div class="form-group"> |
| 147 | + <label asp-for="SomeNumber"></label> |
| 148 | + <kendo-numerictextbox for="SomeNumber" /> |
| 149 | + <span asp-validation-for="SomeNumber" class="text-danger k-invalid-msg" data-for="SomeNumber"></span> |
| 150 | + </div> |
| 151 | + <div class="form-group"> |
| 152 | + <label asp-for="SomeDate"></label> |
| 153 | + <kendo-datetimepicker for="SomeDate" /> |
| 154 | + <span asp-validation-for="SomeDate" class="text-danger k-invalid-msg" data-for="SomeDate"></span> |
| 155 | + </div> |
| 156 | + <div class="form-group"> |
| 157 | + @Html.Kendo().CheckBoxFor(m => m.SomeBoolean) |
| 158 | + <label asp-for="SomeBoolean"></label> |
| 159 | + <span asp-validation-for="SomeBoolean" class="text-danger k-invalid-msg" data-for="SomeBoolean"></span> |
| 160 | + </div> |
| 161 | + <div class="form-group"> |
| 162 | + <input type="submit" value="Save" class="k-button k-primary" /> |
| 163 | + </div> |
| 164 | + </form> |
| 165 | + </div> |
| 166 | +</div> |
| 167 | +
|
| 168 | +<div> |
| 169 | + <a asp-action="Index" class="k-button">Back to List</a> |
| 170 | +</div> |
| 171 | +``` |
| 172 | +```Controller |
| 173 | +using System; |
| 174 | +using System.Collections.Generic; |
| 175 | +using System.Linq; |
| 176 | +using System.Threading.Tasks; |
| 177 | +using Microsoft.AspNetCore.Http; |
| 178 | +using Microsoft.AspNetCore.Mvc; |
| 179 | +using SampleCoreApp.Models; |
| 180 | +
|
| 181 | +namespace SampleCoreApp.Controllers |
| 182 | +{ |
| 183 | + public class TestScaffoldingController : Controller |
| 184 | + { |
| 185 | + //the rest of the controller is omitted for brevity |
| 186 | +
|
| 187 | + // GET: TestScaffolding/Edit/5 |
| 188 | + public ActionResult Edit(int id) |
| 189 | + { |
| 190 | + SampleScaffoldingData sampleData = new SampleScaffoldingData() { |
| 191 | + MultiLineString = string.Format("{0}\n{1}", id, id + 1), |
| 192 | + SingleLineString = id.ToString(), |
| 193 | + SomeBoolean = id % 3 == 0, |
| 194 | + SomeDate = DateTime.Now, |
| 195 | + SomeNumber = id |
| 196 | + }; |
| 197 | +
|
| 198 | + return View(sampleData); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | + |
| 205 | +## See Also |
| 206 | + |
| 207 | +* [Add the Kendo components to Your Project with VS 2017](https://docs.telerik.com/aspnet-core/getting-started/getting-started) |
| 208 | +* [Add the Kendo components to Your Project with the CLI](https://docs.telerik.com/aspnet-core/getting-started/getting-started-cli) |
| 209 | +* [ASP.NET Scaffolding](https://github.com/aspnet/Scaffolding) |
| 210 | +* [Default ASP.NET Scaffolding Templates](https://github.com/aspnet/Scaffolding/tree/master/src/VS.Web.CG.Mvc/Templates) |
| 211 | +* [UI for ASP.NET Core Scaffolding Templates](https://github.com/telerik/scaffold-templates-core) |
| 212 | +* [Tutorial: How to add a model to an ASP.NET Core MVC app](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.1) |
| 213 | +* [Tutorial: How to add a model to a Razor Pages app in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-2.1) |
0 commit comments