Kendo ASP.Net MVC Datagrid how to get Parent Row Id in child rows

1.1k views Asked by At

I have a parent hierarchy grid that have a child table for each of the parent row. I have a custom command button in child table rows and want to access the parent row ID when I press that button. Here is my code :

Parent Table :

    @(Html.Kendo().Grid<FsERP.Models.ParentModel>()
            .Name("gridParent")
            .Columns(columns =>
            {
                columns.Bound(p => p.ID).Hidden(true);
                columns.Bound(p => p.P_Column1).Width(180);
                columns.Bound(p => p.P_Column2).Width(180);
            })
            .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .ServerOperation(false)
            .Model(model =>
            {
                model.Id(p => p.ID);
                model.Field(p => p.ID).Editable(false););
            })
            .PageSize(20)
            .Read(read => read.Action("EditingCustom_Read", "Parent"))
        ).ClientDetailTemplateId("childsTemplate")

Child Table Template :

        <script type="text/kendo" id="childsTemplate">

             @(Html.Kendo().Grid<FsERP.Models.ChildModel>()
                        .Name("gridChild")
                        .Events(e => e.DataBound("OnDataBound").Edit("OnEdit"))
                        .Columns(columns =>
                        {
                            columns.Bound(c => c.ID).Hidden(true);
                            columns.Bound(c => c.C_Column1).Width(180);
                            columns.Bound(c => c.C_Column2).Width(130);
                            columns.Command(command =>command.Custom("GetParentID").Click("showParentID")).Width(80);
    }).DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .ServerOperation(false)
            .Model(model =>
            {
                model.Id(p => p.ID);
                model.Field(p => p.ID).Editable(false););
            })
            .PageSize(20)
            .Read(read => read.Action("EditingCustom_Read", "Child"))
        ).ToClientTemplate()
)

             </script>

Here is my javascript click method :

function showParentID(e) {

}

How would I alert the parent ID inside this showParentID() method. Any help would be appreciable.

Thanks.

NOTE

I dont want to send the parentID in parameter of the javascript method as I also need to access the event information.

1

There are 1 answers

0
Xaimaran On BEST ANSWER

You can add parent ID field into your custom Command name like this:

columns.Command(command => command.Custom("GetParentID_#=ID#")

Then inside your showParentID function retrieve the name of the button which has been triggered and split the ID after the underscore or whatever you rather.

Anyhow this is the POV and you could change this scenario in any form you think is better.

The main hint here is to get access to parent id using #=ID# which is belong to the parent model

Please let me know if it helps or not