I'm using ActionBeginHandler to call the appropriate web service method to add/update data. If there is an error in the service, the new or modified row is still added or updated in the grid. Is there a way to cancel this grid action?
public async void ActionBeginHandler(ActionEventArgs<Application> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
{
if (args.Action == "Add")
{
using var response = await Http.PostAsJsonAsync("application/create", args.Data);
if (!response.IsSuccessStatusCode)
{
// **** How to cancel the grid Add? ****
ShowToast(ToastType.Error, "Error: Unable to create the application.");
}
else
{
Application? application = await response.Content.ReadFromJsonAsync<Application>();
if (application != null)
{
// Update the new application Id for the grid
args.Data.Id = application.Id;
UpdateApplicationRow(application);
ShowToast(ToastType.Success, "Application created successfully.");
}
}
}
else
{
using var response = await Http.PutAsJsonAsync("application/update", args.Data);
if (!response.IsSuccessStatusCode)
{
// **** How to cancel the grid Edit? ****
ShowToast(ToastType.Error, "Error: Unable to update the application.");
}
else
{
Application? application = await response.Content.ReadFromJsonAsync<Application>();
if (application != null && args.Data != null)
{
// Update application in case any data changed from server
UpdateApplicationRow(application);
ShowToast(ToastType.Success, "Application updated successfully.");
}
else
{
ShowToast(ToastType.Error, "Error: Unable to update the Application.");
}
}
}
}
else if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))
{
if (args.Data != null)
{
using var response = await Http.DeleteAsync("application/delete/" + args.Data.Id);
if (!response.IsSuccessStatusCode)
{
ShowToast(ToastType.Error, "Error: Unable to delete the application.");
}
else
{
ShowToast(ToastType.Success, "Application deleted successfully.");
}
}
}
}
private void UpdateApplicationRow(Application? application)
{
if (application == null)
{
return;
}
int? id = application.Id;
if (id != null && applications != null)
{
Application? app = applications.FirstOrDefault(a => a.Id == id);
if (app != null)
{
//app = application;
app.DisplayTags = application.DisplayTags;
if (Grid != null)
{
Grid.Refresh();
}
}
}
}
private void ShowToast(ToastType toastType, string content)
{
if (Toast != null)
{
toastContent = content;
if (toastType == ToastType.Error)
{
toastCssClass = "e-toast-danger";
}
else
{
toastCssClass = "e-toast-success";
}
this.StateHasChanged();
Toast.ShowAsync();
}
}
Thank you - I thought I had tried it without success, but it is working now, Thanks.
|
public async void ActionBeginHandler(ActionEventArgs<Application> args)
{
if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
{
if (args.Action == "Add")
{
using var response = await Http.PostAsJsonAsync("application/create", args.Data);
if (!response.IsSuccessStatusCode)
{
// **** How to cancel the grid Add? ****
args.Cancel = true;
ShowToast(ToastType.Error, "Error: Unable to create the application.");
}
else
{
. . .
}
}
}
else
{
using var response = await Http.PutAsJsonAsync("application/update", args.Data);
if (!response.IsSuccessStatusCode)
{
// **** How to cancel the grid Edit? ****
args.Cancel = true;
ShowToast(ToastType.Error, "Error: Unable to update the application.");
}
else
{
.. .
}
}
}
. . .
}
. . . |
Hi Daniel,
Thanks for the update.
We are happy to hear that the provided solution was helpful to achieve your requirement. Please let back to us if you need further assistance.
Regards,
Rahul