Passing selected rows to controller

Good afternoon.  I am attempting to pass rows/records selected via a checkbox column from my data grid to the MVC controller for further processing.  I was not certain if I could use the data request manager object to process the selected records' collection.  I have attempted multiple variations of Ajax syntax with no success.  Any help would be greatly appreciated.


Thank you!

Doug Riley


4 Replies 1 reply marked as answer

SK Sujith Kumar Rajkumar Syncfusion Team October 11, 2021 05:32 AM UTC

Hi Doug, 
 
Greetings from Syncfusion support. 
 
You can achieve your requirement of passing selected records from Grid to controller method on ajax call using the below code snippet, 
 
// Button click event handler 
document.getElementById('select').addEventListener('click', function (args) { 
        var gridObj = document.getElementById('Grid').ej2_instances[0]; 
        var selectedRecords = gridObj.getSelectedRecords(); 
        if (selectedRecords.length !== 0) { 
            // Ajax post is sent with Grid’s selected records 
            var ajax = new ej.base.Ajax({ 
                url: '/DataGrid/GetSelectedRecords', 
                type: 'POST', 
                contentType: 'application/json; charset=utf-8', 
                data: JSON.stringify(selectedRecords), 
                successHandler: function (data) { 
                    console.log('Returned data: ' + JSON.parse(data).result); 
                } 
            }); 
            ajax.send(); 
        } 
    }) 
 
[Controller part] 
 
[HttpPost] 
// ‘OrderDetails’ class contains definition for all the data that will be received from the client. So this class is used here to access the list of data 
public IActionResult GetSelectedRecords([FromBody]List<OrdersDetails> data) 
{ 
            return Json(new { result = data }); 
} 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 



DR Doug Riley October 13, 2021 03:57 PM UTC

Sujith,

Thank you!  I have downloaded the sample and will be attempting your example shortly.  I will get back to you on the result.


Much appreciated!

Doug



DR Doug Riley October 13, 2021 05:21 PM UTC

Sujith,

How would this solution differ if the grid was set up for paging?

Thank you!

Doug



SK Sujith Kumar Rajkumar Syncfusion Team October 14, 2021 06:50 AM UTC

Hi Doug, 
 
You’re welcome. As for this query – “How would this solution differ if the grid was set up for paging?”, the previous page selected records will not be maintained after paging is performed. So when the selected records are accessed only the current page selected records will be returned. This is the default behavior for this case. 
 
However you can maintain the checkbox selection throughout the pages by enabling the PersistSelection property of the Grid’s SelectionSettings. For persisting selection on the grid, any one of the columns should be defined as a primary key using the columns IsPrimaryKey property. 
 
We have modified the previously shared sample based on this for your reference. Please find it below, 
 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 


Marked as answer
Loader.
Up arrow icon