DxTreeList.EditModelSaving Event
Fires if validation succeeds after a user saves changes or you call the SaveChangesAsync() method.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v24.2.dll
NuGet Package: DevExpress.Blazor
Declaration
[Parameter]
public EventCallback<TreeListEditModelSavingEventArgs> EditModelSaving { get; set; }
Parameters
Type | Description |
---|---|
TreeListEditModelSavingEventArgs | An object that contains data for this event. |
Remarks
The EditModelSaving
event fires when one of the following actions triggers row validation and it succeeds:
- A user submits the edit form or saves the edited row.
- In
EditCell
mode, focus leaves the edited row. - You call the SaveChangesAsync() method.
Set the ValidationEnabled property to false
to disable validation in the TreeList component. When validation is disabled, the actions listed above immediately raise the EditModelSaving
event.
Handle this event to make final data changes, check access permissions, and post changes to the underlying data source. To continue row editing, set the Cancel event argument to true
. The EditModel argument returns the edit model that stores all changes. The IsNew property identifies whether the edit model corresponds to a new or existing data item. Use DataItem and ParentDataItem properties to obtain the processed data item and its parent.
Call the CopyChangesToDataItem() method to copy all changed fields (including nested properties) from the edit model to the data item. This method copies changes regardless of where you apply them: in the event handler, razor code, or business logic.
The following example implements data editing in the TreeList component:
@inject EmployeeTaskService EmployeeTaskService
<DxTreeList Data="TreeListData"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
EditModelSaving="TreeList_EditModelSaving"
DataItemDeleting="TreeList_DataItemDeleting"
CustomizeEditModel="TreeList_CustomizeEditModel">
<Columns>
<DxTreeListCommandColumn />
<DxTreeListDataColumn FieldName="Name" Caption="Task" />
<DxTreeListDataColumn FieldName="EmployeeName" />
<DxTreeListDataColumn FieldName="StartDate" />
<DxTreeListDataColumn FieldName="DueDate" />
</Columns>
</DxTreeList>
@code {
List<EmployeeTask> TreeListData { get; set; }
protected override void OnInitialized() {
TreeListData = EmployeeTaskService.GenerateData();
}
void TreeList_CustomizeEditModel(TreeListCustomizeEditModelEventArgs e) {
if(e.IsNew) {
var newTask = (EmployeeTask)e.EditModel;
newTask.Id = TreeListData.Max(x => x.Id) + 1;
if(e.ParentDataItem != null)
newTask.ParentId = ((EmployeeTask)e.ParentDataItem).Id;
}
}
async Task TreeList_EditModelSaving(TreeListEditModelSavingEventArgs e) {
if(e.IsNew)
TreeListData.Add((EmployeeTask)e.EditModel);
else
e.CopyChangesToDataItem();
}
async Task TreeList_DataItemDeleting(TreeListDataItemDeletingEventArgs e) {
TreeListData.Remove((EmployeeTask)e.DataItem);
}
}
Reload Data
TreeList data should be reloaded after you post updates to the data source. The TreeList component automatically reloads its data in response to the following actions:
- After the
EditModelSaving
or DataItemDeleting event handler is executed. - After you change an instance of a field/property bound to the Data parameter.
In other cases, call the Reload() method to reload TreeList data.
If you call the Reload() method in the EditModelSaving
or DataItemDeleting event handler to refresh data manually, set the Reload
event argument to false
to prevent unnecessary repeated reloads.