How to create a custom child grid column with ActionLinks containing IDs of the child model

3 posts, 1 answers
  1. Mehmet
    Mehmet avatar
    2 posts
    Member since:
    Jan 2011

    Posted 23 May 2013 Link to this post

    Hi,

    how do i add a custom column to a child grid containing a Action Link with the ID of the current child row entity?

    here is a example of the hierarchical grid:
    @(Html.Kendo().Grid<MyParentGridModel>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.Id).Groupable(false).Width(50);
            columns.Bound(p => p.Name);
            columns.Bound(p => p.ShortDescription);
            columns.Bound(p => p.LinkText);
        })
        .Sortable()
        .Pageable()
        .Scrollable(scr=>scr.Height(500))
        .ClientDetailTemplateId("template")
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("ParentModel_Read", "ControllerName"))
        )   
    )
     
    <script id="template" type="text/kendo-tmpl">
        @(Html.Kendo().Grid<ChildGridModel>()
            .Name("Grid_#=Id#")
            .Columns(columns =>
            {
                columns.Bound(o => o.PageNumber).Width(50);
                columns.Bound(o => o.ChapterTitle).Width(150);
                columns.Bound(o => o.BodyContent);
                columns.Template(@<text>
                                @Html.ActionLink("edit", "ControllerAction", "ControllerName", new { id = @item.Id }, new {@class = "k-button" })
                                </text>)
                .ClientTemplate(@Html.ActionLink("edit", "ControllerAction", "ControllerName", new { ID = "#=Id#" }, new { @class = "k-button" }).ToHtmlString());
     
                 
            })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(10)
                .Read(read => read.Action("ChildModel_Read", "ControllerName", new { parentModelId = "#=Id#" }))
            )
            .Pageable()
            .Sortable()
            .ToClientTemplate()
        )
    </script>
    The Question is: How can the Id of the ChildModelEntity be resolved and passed into the ActionLinkMethod?  "#=Id#" returns the Id of the parent model.

    Thanks in advance for answers!
  2. Answer
    Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2289 posts

    Posted 24 May 2013 Link to this post

    Hello Mehmet,

    You must escape the `#` from the child Grid client template:

    columns.ClientTemplate(@Html.ActionLink("edit", "ControllerAction", "ControllerName", new { ID = "\\#=Id\\#" }, new { @class = "k-button" }).ToHtmlString());

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Mehmet
    Mehmet avatar
    2 posts
    Member since:
    Jan 2011

    Posted 27 May 2013 Link to this post

    Hi Nikolay,

    thank you for your reply! This works fine!

Back to Top