# Sending the Selected Grid Rows to the Controller

## Environment

<table>
 <tr>
  <td>Product Version</td>
  <td>2022.3.913</td>
 </tr>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
</table>

## Description

How can I get the selected rows in the Telerik UI for ASP.NET MVC Grid and send them through an AJAX request to the server?

## Solution

1. Create a button and handle its `click` event.
2. Use the client-side [`select()`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/select) method to get the selected row elements.
3. Iterate through the selected rows, retrieve the respective data item through the [`dataItem()`](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/dataitem) method of the Grid, and store it in an array `selectedDataItems`.
4. Convert the data items into strings by using the [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) method.
5. Perform an asynchronous HTTP (Ajax) request to the server.
6. Access the received data on the server.

```Razor HtmlHelper
    @(Html.Kendo().Button()
        .Name("sendDataBtn")
        .Content("Submit selected Grid rows to the server")
        .HtmlAttributes(new { type = "button" })
        .Events(ev => ev.Click("onClick"))
    )

    @(Html.Kendo().Grid<GridModel>()
      .Name("grid")
      .Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
      //Other configuration
    )
    
```

```JavaScript
    function onClick() {
        var grid = $("#grid").data("kendoGrid");
        var selectedDataItems = [];
        var selectedRows = grid.select();
          
        for (var i = 0; i < selectedRows.length; i++) {
          var dataItem = grid.dataItem(selectedRows[i]);
          selectedDataItems.push(dataItem);
        }
        
        var rows = JSON.stringify(selectedDataItems);
        
        $.ajax({
            url: '@Url.Action("GetSelectedRows","Grid")',
            data: rows,
            dataType: "json",
            type: "POST",
            contentType: 'application/json;'
        });
    }
```

```C# GridController
  public ActionResult GetSelectedRows([FromBody] List<GridModel> rows)
  {
    List<GridModel> selecteditems = rows;
    ...
  }

```

## 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)
