Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access row data from within a ClientTemplate within a ClientDetailTemplate

I'm using a Kendo UI Web Grid in an ASP.Net MVC 4 application.

At the bottom of the code below you'll see a JavaScript function called GetEditChildUrl that accepts a parameter called data. Unfortunately, what gets passed in as the data parameter is the parent row data but I was expecting the child row data. This works fine for GetEditParentUrl. So how do I get the child row data?

@(Html.Kendo().Grid<Application.Models.Parent>()
    .Name("grid_parent")
    .Columns(columns =>
    {
        columns.Bound(x => x.Name);
        columns.Bound(x => x).ClientTemplate(
            "<a href='#= GetEditParentUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
          ).Width(90).Filterable(false);
    })
    .Scrollable(action => action.Virtual(true))
    .Filterable()
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Parents_Read", "Parent"))
    )
    .Scrollable(s => s.Height("auto"))
    .ClientDetailTemplateId("client-template")
)

@section Scripts {
    <script id="client-template" type="text/x-kendo-template">
        <h3>Contacts</h3>

        @(Html.Kendo().Grid<Application.Models.Child>()
            .Name("grid_child_#=Id#") // make sure the Name is unique
            .Columns(columns =>
            {
                columns.Bound(x => x.Name);
                columns.Bound(x => x).ClientTemplate(
                    "<a href='#= GetEditChildUrl(data) #' class='k-button' style='min-width:0px;'><span class='k-icon k-i-pencil'></span></a>"
                ).Width(90).Filterable(false);
            })
            .DataSource(dataSource =>
                dataSource.Ajax().Read(read => read.Action("Children_Read", "Parent", new { parentId = "#=Id#" }))
            )
            .Scrollable(s => s.Height("auto"))
            .Sortable()
            .ToClientTemplate()
        )
    </script>

    <script>
        function GetEditParentUrl(data) {
            return "@Url.Action("Edit", "Parent")/" + data.Id;
        }
        function GetEditChildUrl(data) {
            return "@Url.Action("Edit", "Child")/" + data.Id;
        }
    </script>
}
like image 639
Dean Avatar asked Jan 28 '26 23:01

Dean


1 Answers

Try escaping the hash (#) symbols in your column client template.

columns.Bound(x => x)
    .ClientTemplate("<a href='\\#= GetEditChildUrl(data) \\#' ...>")

From the KendoUI docs:

Important: The "#" characters used for a template expression should be escaped when using a column ClientTemplate in a detail template so that the expression is evaluated in the correct context.

like image 200
Kevin Babcock Avatar answered Jan 30 '26 13:01

Kevin Babcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!