Grid Hierarchy, passing the parent ID when creating a new record in a child grid template

1 Answer 29 Views
Grid
Matt
Top achievements
Rank 1
Matt asked on 01 Dec 2023, 05:20 PM
I have a grid for a list of locations, each row expands to show the services at that location. When I click to create a new locationService, I need to pass the locations ID to the controller. The location is the parent grid and location services are the child. I can't figure out how to do this.


    @* LOCATIONS GRID *@
    <div class="row">
        <div class="col-md-12">
            @(Html.Kendo().Grid<BiddingSite.Data.Dtos.LocationDto>
                                        ()
                                        .Name("locationsGrid")
                                        .AutoBind(false)
                                        .Columns(columns =>
                                        {
                                            columns.Bound(c => c.Id).Title("Id").Hidden();
                                            columns.Bound(c => c.Address).Title("Address").Width(25);
                                            columns.Bound(c => c.City).Title("City").Width(15);
                                            columns.Bound(c => c.StateName).Title("State").Width(10);
                                            columns.Bound(c => c.Zip).Title("Zip").Width(10);
                                            columns.Bound(c => c.Number).Title("SiteNumber").Width(10);
                                            columns.Command(command => { command.Edit(); command.Destroy(); }).Width(75);
                                        })
                                        .ToolBar(toolBar =>
                                        {
                                            toolBar.Create();
                                        })
                                        .DataSource(datasource => datasource.Ajax()
                                            .Read(read => read.Action("locations_Read", "OpenBids").Data("getAdditionalParams_Locations_Read"))
                                            .Model(model => model.Id(p => p.Id))
                                            .Create(create => create.Action("Location_Create", "OpenBids").Data("getAdditionalParams_Location_Create"))
                                            .Update(update => update.Action("Location_Update", "OpenBids"))
                                            .Destroy(update => update.Action("Location_Delete", "OpenBids"))
                                        )
                                        .Events(events => events.DetailInit("locationGridEvent_ExpandRow"))
                                        .Sortable()
                                        .Filterable()
                                        .Editable(editable => editable.Mode(GridEditMode.InLine))
                                        .ClientDetailTemplateId("template")
        )
        </div>
    </div>
</div>


@* LOCATION SERVICES GRID *@
<script id="template" type="text/kendo-tmpl">

        @(Html.Kendo().Grid<BiddingSite.Models.ProposalUpdateLocationServiceModel>
                                ()
                                .Name("locationServicesGrid_#=Id#")
                                .Columns(columns =>
                                {
                                    columns.Bound(c => c.TrackingNumber).Title("TrackingNumber").Width(15);
                                    columns.Bound(c => c.Service).Title("Service").Width(100).Locked();
                                    columns.Bound(c => c.ServiceClass).Title("ServiceClass").Width(15);
                                    columns.Bound(c => c.ServiceType).Title("ServiceType").Width(15);
                                    columns.Bound(c => c.EquipmentType).Title("EquipmentType").Width(15);
                                    columns.Bound(c => c.MaterialType).Title("MaterialType").Width(15);
                                    columns.Bound(c => c.Quantity).Title("Quantity").Width(10);
                                    columns.Bound(c => c.ContainerSize).Title("ContainerSize").Width(10);
                                    columns.Bound(c => c.Frequency).Title("Frequency").Width(10);
                                    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
                                })
                                .ToolBar(toolBar =>
                                {
                                    toolBar.Create();
                                })
                                .DataSource(datasource => datasource.Ajax()
                                    .Read("locationServices_Read", "OpenBids", new { id = "#=Id#" })
                                    .Model(model => model.Id(p => p.Id))
                                    .Model(model => model.Field(p => p.Service).Editable(false))
                                    .Create(create => create.Action("LocationService_Create", "OpenBids").Data("getAdditionalParams_LocationService_Create")) // this is where I need to pass the ID of the parent row to have the correct location
                                    .Update(update => update.Action("LocationService_Update", "OpenBids"))
                                    .Destroy(update => update.Action("LocationService_Delete", "OpenBids"))
                                )
                                .Editable(editable => editable.Mode(GridEditMode.PopUp))
                                .Events(e => e.Edit("onEdit"))
                                .ToClientTemplate()
)
</script>

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 06 Dec 2023, 09:13 AM

Hello Matt,

Thank you for the code snippet and the details provided.

In order to achieve the desired behavior, I would recommend using the approach from the following forum thread answer:

Give a try to the approach above and let me know if further assistance is needed.

 

Kind Regards,
Anton Mironov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Anton Mironov
Telerik team
Share this question
or