How to set "ParentId" property on batch edit in ASPNET CORE

Hello team, i wanna  thank you all for the response on another question.

I have one more question... Is there a way when we are "batch editing" a value at the "Add" operation and i need to set its "ParentId" property?

Regards,

Danilo Coimbra.

5 Replies

SE Sathyanarayanamoorthy Eswararao Syncfusion Team May 29, 2018 12:43 PM UTC

Hi Danilo, 

According to your query you need to set the value for the PrimaryKey column on the server side when a new record is added to the grid. We have achieved your requirement in the below code example. 

In the below code example, we have generated a random number and assigned that value to the PrimaryKey Column (OrderID) in server side during the insert operation.  

[index.cshtml] 
@{Html.EJ().Grid<object>("Grid") 
                            .Locale("pt-BR") 
                            .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.datasource).BatchURL("/Home/BatchUpdate").Adaptor(AdaptorType.RemoteSaveAdaptor)) 
                            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().RowPosition(RowPosition.Bottom). 
EditMode(EditMode.Batch).ShowConfirmDialog(false); }) 
 
                                             …... 
 
                            .Columns(col => 
                            { 
                                col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).Visible(false).Add(); 
                                      
                                 …... 
 
                           }) 
                            .Render(); 
} 
[controller.cs] 
 
public IActionResult BatchUpdate([FromBody]List<Order> changed, [FromBody]List<Order> added, [FromBody]List<Order> deleted) 
        { 
            if (changed != null && changed.Count > 0) 
            { 
                foreach (var temp in myObject.Changed) 
                { 
                                       
                    var ord = temp; 
                    Orders val = orddata.Where(or => or.OrderID == ord.OrderID).FirstOrDefault(); 
                    val.OrderID = ord.OrderID; 
                    val.EmployeeID = ord.EmployeeID; 
                    val.CustomerID = ord.CustomerID; 
                    val.OrderDate = ord.OrderDate; 
                    val.ShipCity = ord.ShipCity; 
                } 
            } 
            if (myObject.Added != null && myObject.Added.Count > 0) 
            { 
                Random ran = new Random(); 
                foreach (var temp in myObject.Added) 
                { 
                    temp.OrderID = ran.Next(); 
                    orddata.Insert(0, temp); 
                } 
            }            if (deleted != null && deleted.Count > 0) 
            { 
                foreach (var temp in deleted) 
                { 
                    orddata.Remove(orddata.Where(or => or.OrderID == temp.OrderID).FirstOrDefault()); 
 
                } 
 
            } 
                var data = orddata; 
            return Json(new { added = added, changed = changed, deleted = deleted }); 
 
        } 

Refer the below screenshot. 
 


For your convenience, we have prepared a sample which can be downloaded from the below location. 


If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 




DA Danilo May 29, 2018 01:12 PM UTC

Hi Sathyanarayanamoorthy,
If we need to get the value on"id" element at the view, is there a way to get this? Or querying the repository!?

What is the way that you recommend to achieve that?

Thank you.
Danilo.


SE Sathyanarayanamoorthy Eswararao Syncfusion Team May 30, 2018 01:12 PM UTC

Hi Danilo, 
  
According to your query we suspect that you need to update the Id column in the Client side. We have achieved this requirement using the beforeBatchSave event in the below code example. 

If you want to update the Id column in the server side then we suggest you to follow the solution provided in our previous update.  

 
@{Html.EJ().Grid<object>("Grid") 
                                .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.datasource).BatchURL("/Home/BatchUpdate").Adaptor(AdaptorType.RemoteSaveAdaptor)) 
 
                                  …...                                 
                                 
                                .ClientSideEvents(eve => eve.BeforeBatchSave("Save")) 
                                .Columns(col=> { 
                                                                    col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).Visible(false).Add(); 
                                    col.Field("EmployeeID").HeaderText("EmployeeID").Add(); 
                                    col.Field("CustomerID").HeaderText("CustomerID").Add(); 
                                    col.Field("ShipCity").HeaderText("ShipCity").Add(); 
 
                                }) 
                                .Render(); 
} 
 
 
<script type="text/javascript"> 
    function Save(args) { 
        args.batchChanges.added[0].OrderID = 12345; 
    } 
</script> 

Refer the below screenshot. 

 

Please refer the below documentation for details of beforeBatchSave event. 


We have prepared a sample for your reference which can be downloaded from the below location. 


If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 
 



DA Danilo June 3, 2018 01:05 AM UTC

Hi Sathyanarayanamoorthy,
Everything is working great. Thank you!!


SE Sathyanarayanamoorthy Eswararao Syncfusion Team June 4, 2018 06:46 AM UTC

Hi Danilo, 

We are happy to hear that your issue has been resolved. 
If you need any further assistance please get back to us. 

Regards, 
Sathyanarayanamoorthy 


Loader.
Up arrow icon