# Showing a Message When a Grid Server Request Is Completed

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2023.1.117 version</td>
 </tr>
</table>

## Description

I have a Grid with CRUD operations. Once the `Controller` processes the request, I want to display a message to the user through a JavaScript alert whether the process has succeeded or not.

## Solution

To show a message when a `create`, `update`, or `delete` operation is performed in the Grid:

1. Define a global `successfullOperation` variable to hold the text of the type of the operation.
2. Subscribe and handle the [`RequestEnd` event](https://docs.telerik.com/kendo-ui/api/data/datasource/events/requestend) of the Grid's DataSource. There, set the value of the global variable.
3. Subscribe to the [`Error` event](https://docs.telerik.com/kendo-ui/api/data/datasource/events/error) of the Grid's DataSource.
4. In the handler, set the value of the `successfulOperation` to be an empty string in case an `Error` event is thrown and the operation failes.
5. Subscribe to the [`DataBound` event](https://docs.telerik.com/kendo-ui/api/ui/grid/events/databound) of the Grid.
6. In the `onDataBound` handler, check whether the global variable is set. If set, show the message.

```Razor HtmlHelper
    ...
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("onError").RequestEnd("onEnd"))
        .Model(model => model.Id(p => p.ProductID))
        .Create(update => update.Action("Create", "Grid"))
        .Read(read => read.Action("Read", "Grid"))
        .Update(update => update.Action("Update", "Grid"))
        .Destroy(update => update.Action("Destroy", "Grid"))
    )
    .Events(e=>e.DataBound("onDataBound"))
```

```JavaScript
    var successfullOperation = "";
    function onDataBound(e) {
        if (successfullOperation.length > 0) {
            alert("Row was "+successfullOperation+" successfully.")
        }
        successfullOperation = "";
    }
    function onEnd(e) {
        console.log(e)
        if (e.type == "destroy") {
            successfullOperation = "deleted"
        } else if (e.type == "update") {
            successfullOperation = "updated"
        } else if (e.type == "create") {
            successfullOperation = "created"
        }
    }
    function onError(e) {
        successfullOperation = "";
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert("error");
        }
    }
```

To explore the complete behavior, see the Telerik REPL example on how to [display a message upon a server response to the `Update`, `Create`, and `Destroy` requests performed by the Grid](https://netcorerepl.telerik.com/QduQkAlP18HPzzFR26).

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
