RemoteSaveAdaptor stops working after DataGrid dataSource is changed

Hi,

I am using EJ2 ASP.Net Core Razor page.
I have a page which implements CRUD for Employees using DataGrid. I can add/edit/delete employee records successfully.
I also have a mutiselect above my grid which displays Departments. When user selects one or more departments, the grid datasource is changed in the change event as in the code snippet at the bottom of this post. This works fine too.

However once the dataSource is changed => gridObj.dataSource = data; the CRUD operations on the grid no longer work. When I add an employee, it shows the record added in the grid, but it does not call the handler in the mode, where i have the database save code. And when i refresh the page, the new record is gone.

function multiselectDepartment_change(args) {
        var deptIds = args.value.toString();

        $.ajax({
            url: '/Employee?handler=LoadData',
            method: 'GET',
            data: { "departmentIdsCsv": deptIds },
            success: function (data) {

                var gridObj = document.getElementById('Grid').ej2_instances[0]; //EJ2 Grid instances 

                if (gridObj) {
                    gridObj.dataSource = data;
                }
                else
                    swal("Error find Grid!", "Data not loaded!", "error");
            }
        });
    }

Thanks
Avinash Tauro

7 Replies 1 reply marked as answer

BS Balaji Sekar Syncfusion Team June 8, 2020 04:14 AM UTC

Hi Avinash, 
 
Greetings from the Syncfusion support. 
 
Before proceeding further, we need to clarify that you used CRUD operation with normal edit or batch edit mode such as row or cell basis and please share the insert action code definition of Grid, that will help to further validate it. 
 
Regards, 
Balaji Sekar 



AV Avinash June 8, 2020 07:44 AM UTC

Hi,

I am using normal edit. Please find below the grid markup and the named handler code in my razor page. I have also attached the sample project.

    <ejs-multiselect id="multiselectDepartment" dataSource="@Model.Departments.ToArray()" mode="CheckBox" placeholder="Select Department" popupHeight="220px" 
                     showSelectAll="true" showDropDownIcon="true" enableSelectionOrder="true" filterBarPlaceholder="Search Department" 
                     change="multiselectDepartment_change">
        <e-multiselect-fields value="DeptId" text="DeptName"></e-multiselect-fields>
    </ejs-multiselect>
    
   
   

    <ejs-grid id="Grid" load="onLoad" actionComplete="grid_actionComplete" allowPaging="true" allowFiltering="true" allowSorting="true" allowExcelExport="true"
              toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel", "ExcelExport"})" toolbarClick="toolbarClick">
        <e-grid-filterSettings type="Menu" mode="OnEnter" showFilterBarStatus="true" />
        <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog" allowEditOnDblClick="false" ShowDeleteConfirmDialog="true" />
        <e-data-manager adaptor="RemoteSaveAdaptor"
                        json="@Model.Employees.ToArray()"
                        insertUrl="/Employee?handler=Insert"
                        updateUrl="/Employee?handler=Update"
                        removeUrl="/Employee?handler=Delete" />
        <e-grid-pageSettings pageCount="5" pageSize="5" />
        <e-grid-columns>
            <e-grid-column field="Id" headerText="Emp Id" isPrimaryKey="true" textAlign="Right" width="120" visible="false"></e-grid-column>
            <e-grid-column field="EmpCode" headerText="Emp Code" width="200"></e-grid-column>
            <e-grid-column field="Name" headerText="Name" width="200"></e-grid-column>
            <e-grid-column field="Email" headerText="Email" width="200"></e-grid-column>
            <e-grid-column field="Salary" headerText="Salary" width="200" filter="@(new { type="Excel"})"></e-grid-column>
            <e-grid-column field="DepartmentRefId" headerText="Department" width="200" foreignKeyField="DeptId" foreignKeyValue="DeptName" dataSource="@Model.Departments"></e-grid-column>
            <e-grid-column field="Active" headerText="Active" width="200" editType="booleanedit" displayAsCheckBox="true" textAlign="Center" type="boolean" filter="@(new { type="CheckBox"})"></e-grid-column>
        </e-grid-columns>
    </ejs-grid>

-------------------------------------------------------------------------------------------

        public JsonResult OnPostInsert([FromBody] CRUDModel crudModel)
        {
            var newEmployee = crudModel.Value;
            _db.Employee.Add(newEmployee);
            _db.SaveChanges();
            return new JsonResult(newEmployee);
        }

Thanks
Avinash Tauro


BS Balaji Sekar Syncfusion Team June 9, 2020 09:51 AM UTC

Hi Avinash, 
 
Greeting from Syncfusion. 
 
We have validated the provided information and checked with our end but we are not able to reproduce the reported problem at our sample. We have created a sample in ASP.Net core with Razor pages and used remoteSaveAdaptor and perform CRUD operation in Grid 
 
<ejs-grid id="Grid" allowPaging="true" load="onLoad"  toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})"> 
    <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editsettings> 
    <e-data-manager json="@Model.DataSource.ToArray()" adaptor="RemoteSaveAdaptor" insertUrl="/Index?handler=Insert" updateUrl="/Index?handler=Update" removeUrl="/Index?handler=Delete" ></e-data-manager> 
    <e-grid-pageSettings pageCount="5" pageSize="5"></e-grid-pageSettings> 
    <e-grid-columns> 
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column> 
        . . . . 
    </e-grid-columns> 
</ejs-grid> 
 
    public JsonResult OnPostDataSource([FromBody]Data dm) 
        { 
            var data = OrdersDetails.GetAllRecords(); 
 
            int count = data.Cast<OrdersDetails>().Count(); 
            return dm.requiresCounts ? new JsonResult(new { result = data.Skip(dm.skip).Take(dm.take), count = count }) : new JsonResult(data); 
 
 
        } 
 
         public JsonResult OnPostInsert([FromBody]CRUDModel<OrdersDetails> value) 
        { 
 
            OrdersDetails.GetAllRecords().Insert(0, value.value); 
            return new JsonResult(value.value); 
        } 
 
 

 
 
If you still face the problem then share more details or below information it will helpful for us to provide a better solution as soon as possible.                                                                                                                                               
 
  • Share response (network tab) details while perform update operation in Grid.
  • Did you faced reported problem only when update the record in Grid?
  • Share grid package version for further analysis or share the sample (if possible).
 
Regards, 
Balaji Sekar 



AV Avinash June 13, 2020 09:27 PM UTC

Hi,

The sample you shared does not have a multi select which filters the grid datasource using Ajax.
Please find the my attached sample code in Employee.cshtml.

Thanks
Avinash Tauro

Attachment: DeptEmployees_45efe42.zip


BS Balaji Sekar Syncfusion Team June 15, 2020 11:11 AM UTC

Hi Avinash, 
 
We have analyzed your query with provided the information and we found that cause of the problem as bind Grid dataSource from change event of multiselect dropdown. Grid is consider the dataSource as local instead of RemoteSaveAdaptor. In below code example, we have bind the grid dataSource as grid.dataSource.dataSource.json instead of grid.dataSource property. please refer the below code example and sample for more information. 
[Employee.cshtml] 
   function multiselectDepartment_change(args) { 
        var deptIds = args.value.toString(); 
 
        $.ajax({ 
            url: '/Employee?handler=LoadData', 
            method: 'GET', 
            data: { "departmentIdsCsv": deptIds }, 
            success: function (data) { 
 
                var gridObj = document.getElementById('Grid').ej2_instances[0]; //EJ2 Grid instances  
 
                if (gridObj) { 
                    grid.dataSource.dataSource.json = data; 
                    grid.refreshColumns() 
                } 
                else 
                    swal("Error find Grid!", "Data not loaded!", "error"); 
            } 
        }); 
    } 
 
Now, you can perform CRUD actions after multiselect dropdown bind a data in Syncfusion Grid. 
 
Please get back to us, if you need further assistance. 
 
Regards, 
Balaji Sekar 
 


Marked as answer

AV Avinash June 24, 2020 06:10 AM UTC

Thanks works great


BS Balaji Sekar Syncfusion Team June 24, 2020 07:06 AM UTC

Hi Avinash, 
  
We glad that your issue has been fixed.  
  
Please get back to us if you require further other assistance from us. 
  
Regards, 
Balaji Sekar. 



Loader.
Up arrow icon