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 2016 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:

                .DataSource(dataSource => dataSource
                .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"))
                .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>
                    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"))
                .Pageable(pageable => pageable

    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!

  2. Viktor Tachev
    Viktor Tachev avatar
    2499 posts

    Posted 11 Oct 2016 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.

    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
Back to Top