How to display List<object> Count in a cell

2 posts, 0 answers
  1. Andrew
    Andrew avatar
    39 posts
    Member since:
    Sep 2013

    Posted 09 Oct Link to this post

    In a simple table, I know I'd be able to just call Count in a cell, and there weren't any problems. However, trying to do it in a Grid has been challenging:

    This is how my MVC Grid looks like:

    @(Html.Kendo().Grid<ContentExternalLink>()
                .Name("issue-grid")
                .DataSource(dataSource => dataSource
                .Ajax()
                .Model(model => model.Id(x => x.LinkId))
                .Read(read => read.Action("ContentExternalLink_Read", "ContentManagement"))
                .Create(create => create.Action("ContentExternalLink_Create", "ContentManagement"))
                .Update(update => update.Action("ContentExternalLink_Update", "ContentManagement"))
                .Destroy(delete => delete.Action("ContentExternalLink_Destroy", "ContentManagement"))
                .ServerOperation(true)
                .PageSize(10))
                .Columns(columns =>
                {
                    columns.Bound(x => x.LinkId).Hidden();
                    columns.Bound(x => x.LinkTitle).Title("Title").Template(@<text>
                        <a href="@item.LinkUrl" taget="_blank">@item.LinkTitle</a>
                            </text>);
                    columns.Bound(x => x.LinkTypeId).Title("Type");
                    columns.Bound(x => x.LinkCreatedDate).Title("Created");
                    columns.Bound(x => x.ContentTagAssignments.Count).Title("Tags");
                    columns.Bound(x => x.LinkSource).Hidden();
                    columns.Bound(x => x.LinkPhoneNumber).Hidden();
                    columns.Bound(x => x.LinkDate).Hidden();
                    columns.Bound(x => x.LinkCreatedDate).Hidden();
                    columns.Bound(x => x.LinkModifiedDate).Hidden();
                    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
                })
                .ToolBar(toolbar => toolbar.Create())
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Scrollable(scr => scr.Height("auto"))
                .Sortable()
                .Pageable(pageable => pageable
                    .Refresh(true)
                    .PageSizes(true)
                    .ButtonCount(5))
    )

    When I run that, I get the Javascript error: Cannot read property 'Count' of undefined.

    Any ideas how to get what I want to achieve?

    Thank you!
    AJ

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 11 Oct Link to this post

    Hi Andrew,

    Note that the DataSource is designed to work with flat data. If you would like to show count in one of the Grid columns you can update the Model to include a field that would hold the count value. You can populate that field in the Controller when querying the data from the database. Then , you can show that field as a separate column in the Grid component.

    columns.Bound(x => x.ContentTagAssignmentsCount).Title("Tags");

    On a side note, have in mind that when using Ajax binding you should use ClientTemplate for the columns. Please check out the following article that describes the approach in more detail.




    Regards,
    Viktor Tachev
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top