Parent/Child Grid - Passing parent Grid ID to Child Model

2 posts, 0 answers
  1. Mark
    Mark avatar
    1 posts
    Member since:
    Nov 2011

    Posted 13 Dec 2020 Link to this post

    I'm using UI for ASP.NET core 2020.3.1118, and i'm trying to implement a child grid with the ability to add an entry to my child grid

    I need to pass the Id value from my Parent Grid to the model of the child when i add a new entry
    I've been trying to use the 

    model.Field(gp => gp.GroupId).DefaultValue(0); 

    using a kendo template value "#=id#" in the child grid definition,  without success as the DefaultValue method only seems to allow to accept actual values.

    I'm really looking for advice here,

    i've currently resolved the issue by passing an additional parameter in my Create action and updating the model in my controler with the parameter

    .Create(create => create.Action("Post", "GroupsPermission", new { NewGroupId = "#=Id#" }))

     

    Thanks in Advance

     

     

  2. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    1082 posts

    Posted 16 Dec 2020 Link to this post

    Hello, Mark,

    For a dynamic default value for the new child grid models can be assigned in a BeforeEdit() or Edit() grid event handler function. I assume that you want the child grid grid to have a defaultValue that is taken from the master row and its dataItem, not the parent grid model itself in these snippets below:

    function onBeforeEdit(e){
      if(e.model.isNew()){
          var masterRow = e.sender.wrapper.closest(".k-detail-row").prev();
          var masterDataItem = $("#grid").getKendoGrid().dataItem(masterRow);
          e.model.NewGroupId = masterDataItem.NewGroupId;
       }
    }
    
    // or
    
    function onEdit(e){
      if(e.model.isNew()){
          var masterRow = e.sender.wrapper.closest(".k-detail-row").prev();
          var masterDataItem = $("#grid").getKendoGrid().dataItem(masterRow);
          e.model.set("NewGroupId", masterDataItem.NewGroupId);
       }
    }

    In case my assumption is not correct and you want to use the master grid model as opposed to the master row model, you can obtain the master grid instance and its dataSource model options:

    var masterGridDs = $("#grid").getKendoGrid().dataSource;
    var groupId = masterGridDs.options.schema.model.fields.NewGroupId;

    Regards,
    Alex Hajigeorgieva
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top