This is a migrated thread and some comments may be shown as answers.

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

1 Answer 233 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 2
Mark asked on 13 Dec 2020, 05:55 PM

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

 

 

1 Answer, 1 is accepted

Sort by
0
Alex Hajigeorgieva
Telerik team
answered on 16 Dec 2020, 02:11 PM

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/.

Tags
Grid
Asked by
Mark
Top achievements
Rank 2
Answers by
Alex Hajigeorgieva
Telerik team
Share this question
or