|
| 1 | +--- |
| 2 | +title: Getting Started |
| 3 | +page_title: Getting Started |
| 4 | +description: "Make your first steps with the Telerik UI for {{ site.framework }} ListView component by following a complete step-by-step tutorial." |
| 5 | +slug: listview_getting_started |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting Started with the ListView |
| 10 | + |
| 11 | +This tutorial explains how to set up the Telerik UI for {{ site.framework }} ListView. |
| 12 | + |
| 13 | +You will declare a product view model and bind it to an instance of the ListView component. Next, you will configure the DataSource, the read transport operation for the ListView, and the server endpoint to handle the request at the backend. Finally, you'll add a Kendo template to render the ListView items and will learn how to reference the client-side instance of the component. |
| 14 | + |
| 15 | +After completing this guide, you will achieve the following results: |
| 16 | + |
| 17 | +  |
| 18 | + |
| 19 | +@[template](/_contentTemplates/core/getting-started-prerequisites.md#repl-component-gs-prerequisites) |
| 20 | + |
| 21 | +## 1. Prepare the CSHTML File |
| 22 | + |
| 23 | +@[template](/_contentTemplates/core/getting-started-directives.md#gs-adding-directives) |
| 24 | + |
| 25 | +Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others. |
| 26 | + |
| 27 | +## 2. Declare the View Model |
| 28 | + |
| 29 | +Declare the `ProductViewModel`. |
| 30 | + |
| 31 | +```C# |
| 32 | +using System.ComponentModel.DataAnnotations; |
| 33 | +using System.ComponentModel; |
| 34 | +using System; |
| 35 | + |
| 36 | + public class ProductViewModel |
| 37 | + { |
| 38 | + public int ProductID |
| 39 | + { |
| 40 | + get; |
| 41 | + set; |
| 42 | + } |
| 43 | + |
| 44 | + [Required] |
| 45 | + [Display(Name = "Product name")] |
| 46 | + public string ProductName |
| 47 | + { |
| 48 | + get; |
| 49 | + set; |
| 50 | + } |
| 51 | + |
| 52 | + [Display(Name = "Unit price")] |
| 53 | + [DataType(DataType.Currency)] |
| 54 | + [Range(0, int.MaxValue)] |
| 55 | + public decimal UnitPrice |
| 56 | + { |
| 57 | + get; |
| 58 | + set; |
| 59 | + } |
| 60 | + |
| 61 | + [Display(Name = "Units in stock")] |
| 62 | + [DataType("Integer")] |
| 63 | + [Range(0, int.MaxValue)] |
| 64 | + public int UnitsInStock |
| 65 | + { |
| 66 | + get; |
| 67 | + set; |
| 68 | + } |
| 69 | + |
| 70 | + public bool Discontinued |
| 71 | + { |
| 72 | + get; |
| 73 | + set; |
| 74 | + } |
| 75 | + |
| 76 | + [Display(Name = "Last supply")] |
| 77 | + [DataType(DataType.Date)] |
| 78 | + public DateTime LastSupply |
| 79 | + { |
| 80 | + get; |
| 81 | + set; |
| 82 | + } |
| 83 | + |
| 84 | + [DataType("Integer")] |
| 85 | + public int UnitsOnOrder |
| 86 | + { |
| 87 | + get; |
| 88 | + set; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +``` |
| 93 | + |
| 94 | +## 3. Initialize the ListView |
| 95 | + |
| 96 | +Use the ListView HtmlHelper {% if site.core %}or TagHelper{% endif %} to add the component to a page and set some of its options. |
| 97 | + |
| 98 | +* Use the `Name()` configuration method to assign a name to the instance of the helper—this is mandatory as its value is used for the `id` and the `name` attributes of the outermost ListView element. |
| 99 | +* Add the `DataSource()` configuration option and set the end point for the [read transport]({% slug htmlhelper_datasourcecrud %}). |
| 100 | +* Configure the `TagName` to set the HTML element type used for the outermost element of the component. |
| 101 | +* Set the `PageSize` of the DataSource, and use the `Pageable` property to enable the built-in pager of the ListView. |
| 102 | + |
| 103 | +```HtmlHelper |
| 104 | +@using Kendo.Mvc.UI |
| 105 | +
|
| 106 | + @(Html.Kendo().ListView<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 107 | + .Name("listView") |
| 108 | + .TagName("div") |
| 109 | + .DataSource(dataSource => dataSource |
| 110 | + .Ajax() |
| 111 | + .Read(read => read.Action("Products_Read", "ListView")) |
| 112 | + .PageSize(21) |
| 113 | + ) |
| 114 | + .Pageable() |
| 115 | + ) |
| 116 | +``` |
| 117 | +{% if site.core %} |
| 118 | +```TagHelper |
| 119 | +@addTagHelper *, Kendo.Mvc |
| 120 | +
|
| 121 | +<kendo-listview name="listView" |
| 122 | + tag-name="div"> |
| 123 | + <datasource type="DataSourceTagHelperType.Ajax" page-size="21"> |
| 124 | + <transport> |
| 125 | + <read url="@Url.Action("Products_Read", "ListView")" /> |
| 126 | + </transport> |
| 127 | + </datasource> |
| 128 | + <pageable enabled="true" /> |
| 129 | +</kendo-listview> |
| 130 | +``` |
| 131 | +{% endif %} |
| 132 | + |
| 133 | +## 4. Declare the Read Action |
| 134 | + |
| 135 | +In the `ListView` controller, declare the `Read` action method. Use the name of the action that you set in the DataSource configuration from the previous step. |
| 136 | + |
| 137 | +```Controller |
| 138 | +public ActionResult Index() |
| 139 | +{ |
| 140 | + return View(); |
| 141 | +} |
| 142 | +
|
| 143 | +private List<ProductViewModel> products = new List<ProductViewModel>()new ProductViewModel { |
| 144 | + new ProductViewModel { ProductID = 1, ProductName = "Chai", UnitPrice = 18, UnitsInStock = 39, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 145 | + new ProductViewModel { ProductID = 2, ProductName = "Chang", UnitPrice = 19, UnitsInStock = 17, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 40 }, |
| 146 | + new ProductViewModel { ProductID = 3, ProductName = "Aniseed Syrup", UnitPrice = 10, UnitsInStock = 13, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 70 }, |
| 147 | + new ProductViewModel { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", UnitPrice = 22, UnitsInStock = 53, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 148 | + new ProductViewModel { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", UnitPrice = 21.35, UnitsInStock = 0, Discontinued = true, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 149 | + new ProductViewModel { ProductID = 6, ProductName = "Grandma's Boysenberry Spread", UnitPrice = 25, UnitsInStock = 120, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 150 | + new ProductViewModel { ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", UnitPrice = 30, UnitsInStock = 15, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 151 | + new ProductViewModel { ProductID = 8, ProductName = "Northwoods Cranberry Sauce", UnitPrice = 40, UnitsInStock = 6, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 152 | + new ProductViewModel { ProductID = 9, ProductName = "Mishi Kobe Niku", UnitPrice = 97, UnitsInStock = 29, Discontinued = true, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 153 | + new ProductViewModel { ProductID = 10, ProductName = "Ikura", UnitPrice = 31, UnitsInStock = 31, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 154 | + new ProductViewModel { ProductID = 11, ProductName = "Queso Cabrales", UnitPrice = 21, UnitsInStock = 22, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 30 }, |
| 155 | + new ProductViewModel { ProductID = 12, ProductName = "Queso Manchego La Pastora", UnitPrice = 38, UnitsInStock = 86, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 156 | + new ProductViewModel { ProductID = 13, ProductName = "Konbu", UnitPrice = 6, UnitsInStock = 24, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 157 | + new ProductViewModel { ProductID = 14, ProductName = "Tofu", UnitPrice = 23.25, UnitsInStock = 35, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 158 | + new ProductViewModel { ProductID = 15, ProductName = "Genen Shouyu", UnitPrice = 15.5, UnitsInStock = 39, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 159 | + new ProductViewModel { ProductID = 16, ProductName = "Pavlova", UnitPrice = 17.45, UnitsInStock = 29, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 160 | + new ProductViewModel { ProductID = 17, ProductName = "Alice Mutton", UnitPrice = 39, UnitsInStock = 0, Discontinued = true, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 161 | + new ProductViewModel { ProductID = 18, ProductName = "Carnarvon Tigers", UnitPrice = 62.5, UnitsInStock = 42, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 162 | + new ProductViewModel { ProductID = 19, ProductName = "Teatime Chocolate Biscuits", UnitPrice = 9.2, UnitsInStock = 25, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 163 | + new ProductViewModel { ProductID = 20, ProductName = "Sir Rodney's Marmalade", UnitPrice = 81, UnitsInStock = 40, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 0 }, |
| 164 | + new ProductViewModel { ProductID = 21, ProductName = "Sir Rodney's Scones", UnitPrice = 10, UnitsInStock = 3, Discontinued = false, LastSupply = new DateTime(2023, 03, 10, 00, 00, 00), UnitsOnOrder = 40 } |
| 165 | +} |
| 166 | +
|
| 167 | +public virtual JsonResult Basic_Usage_Read([DataSourceRequest] DataSourceRequest request) |
| 168 | +{ |
| 169 | + return Json(products.ToDataSourceResult(request)); |
| 170 | +} |
| 171 | +
|
| 172 | +``` |
| 173 | + |
| 174 | +## 5. Add a Template to Render the ListView Items |
| 175 | + |
| 176 | +The ListView expects a mandatory `ClientTemplateId` configuration to render the data items received from the server. |
| 177 | + |
| 178 | +```HtmlHelper |
| 179 | +@using Kendo.Mvc.UI |
| 180 | +
|
| 181 | + @(Html.Kendo().ListView<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 182 | + .Name("listView") |
| 183 | + .TagName("div") |
| 184 | + .ClientTemplateId("template") |
| 185 | + .DataSource(dataSource => dataSource |
| 186 | + .Ajax() |
| 187 | + .Read(read => read.Action("Products_Read", "ListView")) |
| 188 | + .PageSize(21) |
| 189 | + ) |
| 190 | + .Pageable() |
| 191 | + ) |
| 192 | +``` |
| 193 | +{% if site.core %} |
| 194 | +```TagHelper |
| 195 | +@addTagHelper *, Kendo.Mvc |
| 196 | +
|
| 197 | +<kendo-listview name="listView" |
| 198 | + tag-name="div" |
| 199 | + template-id="template"> |
| 200 | + <datasource type="DataSourceTagHelperType.Ajax" page-size="21"> |
| 201 | + <transport> |
| 202 | + <read url="@Url.Action("Products_Read", "ListView")" /> |
| 203 | + </transport> |
| 204 | + </datasource> |
| 205 | + <pageable enabled="true" /> |
| 206 | +</kendo-listview> |
| 207 | +``` |
| 208 | +{% endif %} |
| 209 | +```Template |
| 210 | + <script type="text/x-kendo-tmpl" id="template"> |
| 211 | + <div class="product"> |
| 212 | + <img src="@Url.Content("~/shared/web/foods/")#:ProductID#.jpg" alt="#:ProductName# image" /> |
| 213 | + <h3>#:ProductName#</h3> |
| 214 | + <p>#:kendo.toString(UnitPrice, "c")#</p> |
| 215 | + </div> |
| 216 | + </script> |
| 217 | +``` |
| 218 | + |
| 219 | +## (Optional) Reference Existing ListView Instances |
| 220 | + |
| 221 | +Referencing existing component instances allows you to build on top of their configuration. To reference an existing ListView instance, use the [`jQuery.data()`](http://api.jquery.com/jQuery.data/) method. Once a reference is established, use the [ListView client-side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/listview#methods) to control its behavior. |
| 222 | + |
| 223 | +1. Use the `id` attribute of the component instance to establish a reference. |
| 224 | + |
| 225 | + ```script |
| 226 | + <script> |
| 227 | + var listviewReference = $("#listView").data("kendoListView"); // listviewReference is a reference to the existing instance of the helper. |
| 228 | + </script> |
| 229 | + ``` |
| 230 | +
|
| 231 | +1. Use the [ListView client-side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/listview#methods) to control the behavior of the widget. In this example, you will see how to turn on the [`selectable`](https://docs.telerik.com/kendo-ui/api/javascript/ui/listview/configuration/selectable) configuration with the use of the [`setOptions`](https://docs.telerik.com/kendo-ui/api/javascript/ui/widget/methods/setoptions) method. Then you can use the [`select`](https://docs.telerik.com/kendo-ui/api/javascript/ui/listview/methods/select) method to programmatically select one of the items. |
| 232 | +
|
| 233 | +```script |
| 234 | + <script> |
| 235 | + var listview = $("#listView").data("kendoListView"); |
| 236 | + listview.setOptions({selectable: "single"}); // Turn on the selectable mode of the ListView. |
| 237 | + listview.select(listview.content.children().first()); // Select the first item. |
| 238 | + </script> |
| 239 | +``` |
| 240 | + |
| 241 | +{% if site.core %} |
| 242 | +## Explore this Tutorial in REPL |
| 243 | + |
| 244 | +You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: |
| 245 | + |
| 246 | +* [Sample code with the ListView HtmlHelper](https://netcorerepl.telerik.com/cRYdvubb06kr7jZv50) |
| 247 | +* [Sample code with the ListView TagHelper](https://netcorerepl.telerik.com/QdExlOPl09DQHvVp44) |
| 248 | + |
| 249 | +{% endif %} |
| 250 | + |
| 251 | +## Next Steps |
| 252 | + |
| 253 | +* [ListView Editing]({% slug htmlhelpers_listview_aspnetcore_editing %}) |
| 254 | +* [Subscribing to the ListView's Events]({% slug listview_events %}) |
| 255 | +* [ListView Selection Modes]({% slug htmlhelpers_listview_aspnetcore_selection %}) |
| 256 | + |
| 257 | +## See Also |
| 258 | + |
| 259 | +* [Using the API of the ListView for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/listview/api) |
| 260 | +* [Client-Side API of the ListView](https://docs.telerik.com/kendo-ui/api/javascript/ui/listview) |
| 261 | +* [Server-Side API of the ListView](/api/listview) |
| 262 | +* [Knowledge Base Section](/knowledge-base) |
0 commit comments