|
| 1 | +--- |
| 2 | +title: Ajax Binding |
| 3 | +page_title: Ajax Binding |
| 4 | +description: "Learn how to configure the Telerik UI Gantt for {{ site.framework }} to use Ajax binding" |
| 5 | +slug: htmlhelpers_gantt_ajaxbinding_aspnetcore |
| 6 | +position: 1 |
| 7 | +--- |
| 8 | + |
| 9 | +# Ajax Binding |
| 10 | + |
| 11 | +You can bind the Telerik UI Gantt for {{ site.framework }} using Ajax binding to load and process tasks and dependencies through server-side Action methods. |
| 12 | + |
| 13 | +For a complete example, refer to the [Basic Usage Demo of the Gantt component](https://demos.telerik.com/{{ site.platform }}/gantt/basic-usage). |
| 14 | + |
| 15 | +To configure the Gantt for Ajax data binding, follow the next steps: |
| 16 | + |
| 17 | +1. Set up the Gantt’s DataSource configuration |
| 18 | + - Specify the action methods for the CRUD operations in the `Read`, `Create`, `Update`, and `Destroy` options of the `DataSource` and `DependencyDataSource` configurations. |
| 19 | + - Define the unique Model identifier in the `Id()` option of the `Model` configuration of each DataSource. In addition, map the required Model fields, such as `ParentID`, `Start`, `End`, `Expanded`, and more, in the `Model` configuration. |
| 20 | + - Configure the additional Gantt settings like views, columns, and more. |
| 21 | + |
| 22 | +```HtmlHelper |
| 23 | + @using Kendo.Mvc.Examples.Models.Gantt; |
| 24 | + |
| 25 | + @(Html.Kendo().Gantt<TaskViewModel, DependencyViewModel>() |
| 26 | + .Name("gantt") |
| 27 | + .Columns(columns => |
| 28 | + { |
| 29 | + columns.Bound(c => c.TaskID).Title("ID").Width(50); |
| 30 | + columns.Bound(c => c.Title).Editable(true).Sortable(true); |
| 31 | + columns.Group(g => |
| 32 | + { |
| 33 | + g.Bound(c => c.Start).Width(100).Editable(true).Sortable(true); |
| 34 | + g.Bound(c => c.End).Width(100).Editable(true).Sortable(true); |
| 35 | + }).Title("Timings"); |
| 36 | + }) |
| 37 | + .Views(views => |
| 38 | + { |
| 39 | + views.DayView(); |
| 40 | + views.WeekView(weekView => weekView.Selected(true)); |
| 41 | + views.MonthView(); |
| 42 | + }) |
| 43 | + .Height(700) |
| 44 | + .ShowWorkHours(false) |
| 45 | + .ShowWorkDays(false) |
| 46 | + .Snap(false) |
| 47 | + .DataSource(d => d |
| 48 | + .Model(m => |
| 49 | + { |
| 50 | + m.Id(f => f.TaskID); |
| 51 | + m.ParentId(f => f.ParentID); |
| 52 | + m.Field(f => f.Expanded).DefaultValue(true); |
| 53 | + }) |
| 54 | + .Read("Basic_Usage_ReadTasks", "Gantt") |
| 55 | + .Destroy("Basic_Usage_DestroyTask", "Gantt") |
| 56 | + .Update(update => update.Action("Basic_Usage_UpdateTask", "Gantt").Data("onUpdateCreate")) |
| 57 | + .Create(create => create.Action("Basic_Usage_CreateTask", "Gantt").Data("onUpdateCreate")) |
| 58 | + ) |
| 59 | + .DependenciesDataSource(d => d |
| 60 | + .Model(m => |
| 61 | + { |
| 62 | + m.Id(f => f.DependencyID); |
| 63 | + m.PredecessorId(f => f.PredecessorID); |
| 64 | + m.SuccessorId(f => f.SuccessorID); |
| 65 | + }) |
| 66 | + .Read("Basic_Usage_ReadDependencies", "Gantt") |
| 67 | + .Create("Basic_Usage_CreateDependency", "Gantt") |
| 68 | + .Destroy("Basic_Usage_DestroyDependency", "Gantt") |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + <script> |
| 73 | + // Send the dates for the newly creted/updated tasks as UTC strings |
| 74 | + function onUpdateCreate(e) { |
| 75 | + e.End = e.End.toISOString(); |
| 76 | + e.Start = e.Start.toISOString(); |
| 77 | + } |
| 78 | + </script> |
| 79 | +``` |
| 80 | + |
| 81 | +{% if site.core %} |
| 82 | + |
| 83 | +```TagHelper |
| 84 | + @addTagHelper *, Kendo.Mvc |
| 85 | + |
| 86 | + <kendo-gantt snap="false" height="700" show-work-days="false" show-work-hours="false" name="gantt"> |
| 87 | + <columns> |
| 88 | + <gantt-column field="TaskID" title="ID" width="50px"> |
| 89 | + </gantt-column> |
| 90 | + <gantt-column editable="true" field="title" title="Title" width="255px"> |
| 91 | + </gantt-column> |
| 92 | + <gantt-column title="Timings"> |
| 93 | + <columns> |
| 94 | + <gantt-column field="start" editable="true" title="Start Date" width="100"> |
| 95 | + <sortable enabled="true"/> |
| 96 | + </gantt-column> |
| 97 | + <gantt-column field="end" editable="true" title="End Date" width="100"> |
| 98 | + <sortable enabled="true" /> |
| 99 | + </gantt-column> |
| 100 | + </columns> |
| 101 | + </gantt-column> |
| 102 | + </columns> |
| 103 | + <views> |
| 104 | + <gantt-view type="GanttViewType.Day"> |
| 105 | + </gantt-view> |
| 106 | + <gantt-view selected="true" type="GanttViewType.Week"> |
| 107 | + </gantt-view> |
| 108 | + <gantt-view type="GanttViewType.Month"> |
| 109 | + </gantt-view> |
| 110 | + </views> |
| 111 | + <gantt-datasource type="DataSourceTagHelperType.Ajax"> |
| 112 | + <schema data="Data" total="Total" errors="Errors"> |
| 113 | + <model id="TaskID" parent-id=""> |
| 114 | + <fields> |
| 115 | + <field name="TaskID" type="number"></field> |
| 116 | + <field name="parentId" from="ParentID" type="number" default-value="null"></field> |
| 117 | + <field name="title" from="Title" type="string"></field> |
| 118 | + <field name="start" from="Start" type="date"></field> |
| 119 | + <field name="end" from="End" type="date"></field> |
| 120 | + <field name="summary" from="Summary" type="boolean"></field> |
| 121 | + <field name="expanded" from="Expanded" type="boolean" default-value="true"></field> |
| 122 | + <field name="percentComplete" from="PercentComplete" type="number"></field> |
| 123 | + <field name="orderId" from="OrderId" type="number"></field> |
| 124 | + </fields> |
| 125 | + </model> |
| 126 | + </schema> |
| 127 | + <transport > |
| 128 | + <read url="@Url.Action("Basic_Usage_ReadTasks","Gantt")" /> |
| 129 | + <update data="onUpdateCreate" url="@Url.Action("Basic_Usage_UpdateTask","Gantt")" /> |
| 130 | + <create data="onUpdateCreate" url="@Url.Action("Basic_Usage_CreateTask","Gantt")" /> |
| 131 | + <destroy url="@Url.Action("Basic_Usage_DestroyTask","Gantt")" /> |
| 132 | + </transport> |
| 133 | + </gantt-datasource> |
| 134 | + <dependency-datasource name="dependencies" type="DataSourceTagHelperType.Ajax"> |
| 135 | + <transport> |
| 136 | + <read url="@Url.Action("Basic_Usage_ReadDependencies", "Gantt")" /> |
| 137 | + <create url="@Url.Action("Basic_Usage_CreateDependency", "Gantt")" /> |
| 138 | + <destroy url="@Url.Action("Basic_Usage_DestroyDependency", "Gantt")" /> |
| 139 | + </transport> |
| 140 | + <schema> |
| 141 | + <model id="DependencyID"> |
| 142 | + <fields> |
| 143 | + <field name="DependencyID" type="number"></field> |
| 144 | + <field name="predecessorId" from="PredecessorID" type="number"></field> |
| 145 | + <field name="successorId" from="SuccessorID" type="number"></field> |
| 146 | + <field name="type" from="Type" type="number"></field> |
| 147 | + </fields> |
| 148 | + </model> |
| 149 | + </schema> |
| 150 | + </dependency-datasource> |
| 151 | + </kendo-gantt> |
| 152 | + <script> |
| 153 | + // Send the dates for the newly creted/updated tasks as UTC strings |
| 154 | + function onUpdateCreate(e) { |
| 155 | + e.End = e.End.toISOString(); |
| 156 | + e.Start = e.Start.toISOString(); |
| 157 | + } |
| 158 | + </script> |
| 159 | +``` |
| 160 | +{% endif %} |
| 161 | + |
| 162 | +2. Define the Action methods in the Controller |
| 163 | + * Create action methods for reading, creating, updating, and deleting tasks and dependencies. |
| 164 | + * Each action method must return a JSON result using the `ToDataSourceResult(request, ModelState)` method for proper binding and validation handling. |
| 165 | + |
| 166 | +```Controller |
| 167 | + public partial class GanttController : Controller |
| 168 | + { |
| 169 | + public ActionResult Basic_Usage() |
| 170 | + { |
| 171 | + return View(); |
| 172 | + } |
| 173 | +
|
| 174 | + public virtual JsonResult Basic_Usage_ReadTasks([DataSourceRequest] DataSourceRequest request) |
| 175 | + { |
| 176 | + return Json(taskService.GetAll().ToDataSourceResult(request)); |
| 177 | + } |
| 178 | +
|
| 179 | + public virtual JsonResult Basic_Usage_DestroyTask([DataSourceRequest] DataSourceRequest request, TaskViewModel task) |
| 180 | + { |
| 181 | + if (ModelState.IsValid) |
| 182 | + { |
| 183 | + taskService.Delete(task, ModelState); |
| 184 | + } |
| 185 | +
|
| 186 | + return Json(new[] { task }.ToDataSourceResult(request, ModelState)); |
| 187 | + } |
| 188 | +
|
| 189 | + public virtual JsonResult Basic_Usage_CreateTask([DataSourceRequest] DataSourceRequest request, TaskViewModel task) |
| 190 | + { |
| 191 | + if (ModelState.IsValid) |
| 192 | + { |
| 193 | + taskService.Insert(task, ModelState); |
| 194 | + } |
| 195 | +
|
| 196 | + return Json(new[] { task }.ToDataSourceResult(request, ModelState)); |
| 197 | + } |
| 198 | +
|
| 199 | + public virtual JsonResult Basic_Usage_UpdateTask([DataSourceRequest] DataSourceRequest request, TaskViewModel task) |
| 200 | + { |
| 201 | + if (ModelState.IsValid) |
| 202 | + { |
| 203 | + taskService.Update(task, ModelState); |
| 204 | + } |
| 205 | +
|
| 206 | + return Json(new[] { task }.ToDataSourceResult(request, ModelState)); |
| 207 | + } |
| 208 | +
|
| 209 | + public virtual JsonResult Basic_Usage_ReadDependencies([DataSourceRequest] DataSourceRequest request) |
| 210 | + { |
| 211 | + return Json(dependencyService.GetAll().ToDataSourceResult(request)); |
| 212 | + } |
| 213 | +
|
| 214 | + public virtual JsonResult Basic_Usage_DestroyDependency([DataSourceRequest] DataSourceRequest request, DependencyViewModel dependency) |
| 215 | + { |
| 216 | + if (ModelState.IsValid) |
| 217 | + { |
| 218 | + dependencyService.Delete(dependency); |
| 219 | + } |
| 220 | +
|
| 221 | + return Json(new[] { dependency }.ToDataSourceResult(request, ModelState)); |
| 222 | + } |
| 223 | +
|
| 224 | + public virtual JsonResult Basic_Usage_CreateDependency([DataSourceRequest] DataSourceRequest request, DependencyViewModel dependency) |
| 225 | + { |
| 226 | + if (ModelState.IsValid) |
| 227 | + { |
| 228 | + dependencyService.Insert(dependency); |
| 229 | + } |
| 230 | +
|
| 231 | + return Json(new[] { dependency }.ToDataSourceResult(request, ModelState)); |
| 232 | + } |
| 233 | + } |
| 234 | +``` |
| 235 | + |
| 236 | +## See Also |
| 237 | + |
| 238 | +* [Gantt Basic Usage Demo](https://demos.telerik.com/{{ site.platform }}/gantt/basic-usage) |
| 239 | +* [Gantt Server-Side API](/api/gantt) |
| 240 | +{% if site.core %} |
| 241 | +* [Server-Side TagHelper API of the Gantt](/api/taghelpers/gantt) |
| 242 | +{% endif %} |
0 commit comments