Prevent Adding/Updating Row on Web Service Failure

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();
    }
}

3 Replies

DH Daniel Hulse replied to Rahul Narayanasamy March 28, 2022 02:28 PM UTC

Thank you - I thought I had tried it without success, but it is working now, Thanks.




RN Rahul Narayanasamy Syncfusion Team March 28, 2022 02:58 PM UTC

Hi Daniel, 

Greetings from Syncfusion. 

You want to cancel the default save operation while saving the edited record. You can achieve your requirement by using args.Cancel argument of OnActionBegin. You can set args.Cancel as true to prevent the default operations. Find the below code snippets for your reference. 


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 
            { 
                .. . 
            } 
        } 
    } 
    . . . 
. . . 

Please let us know if you have any concerns. 

Regards, 
Rahul 



RN Rahul Narayanasamy Syncfusion Team replied to Daniel Hulse March 29, 2022 12:28 PM UTC

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


Loader.
Up arrow icon