Update datasource detail grid

Hi,
I have a master-detail grid with CRUD functionality on detail grid, when I insert new item in detail grid, all work well and new record is seen in the grid, but if I return to the same record after seeing the detail of another, the new record is no longer seen. The problem would seem to be due to the fact that the DataSource of the detail grid does not update with the new record, come I can fix it?

public async Task<ActionResult> CrudPartnerServizi(ServiziUtenteVM value, Guid? key, string action)
       {
 
           log4net.Config.XmlConfigurator.Configure();
           log.Info("Inizio esecuzione CrudPartnerServizi(ServiziClienteVM value, Guid? key, string action)");
           try
           {
 
               if (action == "update")
               {
                   if (ModelState.IsValid)
                   {
                       Servizio servizio = new Servizio();
                       Guid IdUtente = Guid.Parse(Request.Headers.GetValues("IdUtente")[0]);
                       servizio = await _servizioRepository.GetServizio(false, value.IDServizio);
                       value.IDUtente = IdUtente;
                       value.NomeServizio = servizio.NomeServizio;
 
                       ServizioCliente servizioCliente = new ServizioCliente()
                       {
                           ID = value.ID,
                           IDCliente = value.IDUtente,
                           IDServizio = value.IDServizio
                       };
 
                       if (await _servizioClienteRepository.ExistServizioCliente(value.IDUtente, value.IDServizio))
                       {
                           HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(Resources.Servizi.ServizioPresente));
                           Response.StatusCode = 412;
                       }
                       else
                       {
                           if (await _servizioClienteRepository.UpdServizioCliente(servizioCliente) != 1)
                           {
                               HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(Resources.Common.ErroreTrasmissione));
                               Response.StatusCode = 412;
                           }
                       }
                   }
                   else
                   {
                       var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
                       HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(message));
                       Response.StatusCode = 412;
                   }
               }
               else if (action == "insert")
               {
                   ModelState.Remove("value.ID");
                   if (ModelState.IsValid)
                   {
                       Guid IdUtente = Guid.Parse(Request.Headers.GetValues("IdUtente")[0]);
                       Servizio servizio = new Servizio();
                       servizio = await _servizioRepository.GetServizio(false, value.IDServizio);
                       value.ID = Guid.NewGuid();
                       value.IDUtente = IdUtente;
                       value.NomeServizio = servizio.NomeServizio;
 
                       ServizioCliente servizioCliente = new ServizioCliente()
                       {
                           ID = value.ID,
                           IDCliente = value.IDUtente,
                           IDServizio = value.IDServizio
                       };
 
                       if (await _servizioClienteRepository.ExistServizioCliente(value.IDUtente, value.IDServizio))
                       {
                           HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(Resources.Servizi.ServizioPresente));
                           Response.StatusCode = 412;
                       }
                       else
                       {
                           if (await _servizioClienteRepository.InsServizioCliente(servizioCliente) != 1)
                           {
                               HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(Resources.Common.ErroreTrasmissione));
                               Response.StatusCode = 412;
                           }
                       }
                   }
                   else
                   {
                       var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));
                       HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(message));
                       Response.StatusCode = 412;
                   }
               }
               else if (action == "remove")
               {
                   if (key == nullreturn new
                     HttpStatusCodeResult(HttpStatusCode.BadRequest);
 
                   await _servizioClienteRepository.DelServizioCliente(key);
               }
               ViewBag.ServiziPartner = await LoadServiziUtente();
               log.Info("Fine esecuzione CrudServizi(CrudPartnerServizi value, Guid? key, string action)");
           }
           catch (Exception ex)
           {
 
               log.Error(string.Format("Errore:{0}", ex.InnerException == null ? ex.Message : ex.InnerException.Message));
               var message = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
               HttpContext.Response.AddHeader("Exception", Server.HtmlEncode(message));
               Response.StatusCode = 412;
           }
           return Json(value, JsonRequestBehavior.AllowGet);
       }
RowSelected event of master grid
function RowSelected(args) {
            var IdUtente = args.data.IDUtente;
            var servizidata = ej.DataManager(@Html.Raw(Json.Encode(ViewBag.ServiziPartner))).executeLocal(ej.Query().where("IDUtente", ej.FilterOperators.equal, IdUtente, false));
               var servizigridObj = $("#PartnerServiziGrid").ejGrid("instance");
               servizigridObj.dataSource(ej.DataManager({ json: servizidata, crudUrl: "@(Url.Action("CrudPartnerServizi","Admin"))", adaptor: "remoteSaveAdaptor"}));
               servizigridObj.model.dataSource.dataSource.headers = [];
               servizigridObj.model.dataSource.dataSource.headers.push({ "IdUtente": IdUtente });
 
        }





1 Reply

MS Mani Sankar Durai Syncfusion Team January 16, 2018 12:22 PM UTC

Hi Pio, 

Thanks for contacting Syncfusion support.  

We have checked your query and we are able to reproduce the reported issue.  The cause of the issue is due to settings the ViewBag value to the grid dataSource in the rowSelected event which will set the data at the initial time only. So when performing CRUD operation the added data will not be in ViewBag.ServiziPartner and hence the added data is not shown in grid after viewing further time.  
To achieve based on your requirement we suggest you to bind the updated data to the grid dataSource.  
We have also prepared a sample that can be downloaded from the below link 
Please refer the below code example 
@(Html.EJ().Grid<object>("MasterGrid") 
... 
        .Columns(col => 
        { 
            ... 
         
        }) 
        .ClientSideEvents(eve => { eve.RowSelected("rowSelected"); }) 
) 
 
<div class="label1"> 
    Detail Grid 
</div> 
@(Html.EJ().Grid<object>("DetailGrid") 
... 
        .Columns(col => 
        { 
... 
 
        }) 
) 
 
<script type="text/javascript"> 
    var data = @Html.Raw(Json.Encode(ViewBag.dataSource1)); 
    var empvalue; 
        function rowSelected(args) { 
            empvalue = args.data.EmployeeID; 
            $.ajax({ 
                type: "POST", 
                url: "/Home/DataSource", 
                success: function (result) { 
                    var employeeID = empvalue; 
                    var detaildata = ej.DataManager(result).executeLocal(ej.Query().where("EmployeeID", ej.FilterOperators.equal, employeeID, false).take(5)); 
                    var gridObj = $("#DetailGrid").ejGrid("instance"); 
                    gridObj.dataSource(ej.DataManager({ json: detaildata, crudUrl: "@(Url.Action("CRUDUpdate","Home"))", adaptor: "remoteSaveAdaptor" }));   
 
                } 
            }); 
 
        } 
</script> 
 
 
[HomeController.cs] 
  public ActionResult CRUDUpdate(EditableOrder value, string action) 
        { 
... 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
        public ActionResult DataSource() 
        { 
            var data = OrderRepository.GetAllRecords().ToList(); 
            return Json(data, JsonRequestBehavior.AllowGet);  //return the update data to the client side  
        } 


From the above code example using AJAX post, return the updated data source to the grid instead of binding from ViewBag.  

Please let us know if you need further assistance.  

Regards, 
Manisankar Durai. 



Loader.
Up arrow icon