Column count for related table

2 posts, 0 answers
  1. Dario
    Dario avatar
    40 posts
    Member since:
    Jun 2018

    Posted 26 Oct 2020 Link to this post

    Hi to all,

    I have this collection routine

    contacts = from rec in _context.Contacts.Include("SellToContacts")
                               where rec.ParentContactFK.HasValue == false
                               select rec;
    DataSourceResult result = contacts.ToDataSourceResult(request);
     
    return Json(result);

    Contact model is configured in this mode

    [...]
    public Contact ContactParent { get; set; }
             
    [Column("Parent Contact Id", TypeName = "int")]
    [Display(Name = "Società Principale")]
    public int? ParentContactFK { get; set; }
     
    [ForeignKey("ParentContactFK")]
    public ICollection<Contact> SellToContacts{ get; set; }
    [...]

    with this collection I would show a column that shows a Count of SellToContacts, that there are children of primary contact.

    If a try to debug a collection, I found , correctly, some SellToContacts loaded of several children.

    [...]
    @(Html.Kendo().Grid<Portale.Web2.Data.Entities.Contact>
        ()
        .Name("contactGrid")
        .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("GetContacts", "Contacts").Data("addInfo"))
        )
        .Columns(columns =>
        {
            columns.Bound(c => c.Date).Format("{0:dd/MM/yyyy}");
            columns.Bound(c => c.Name);
            columns.Bound(c => c.SellToContacts.Count);
     
    [...]

    Why not appears into a grid?

    Where I wrong?

  2. Georgi Denchev
    Admin
    Georgi Denchev avatar
    86 posts

    Posted 29 Oct 2020 Link to this post

    Hello Dario,

     

    Thank you for the provided code snippets.

    The kendo grid columns can be bound to properties of an object(Model). In this case we are attempting to bind the column to a value of a property, which is an incorrect configuration. There are a few options to solve this issue.

    Solution 1 - create a SellToContactsCount property in the Model and obtain the count of the list there. Then bind the column to that property instead of the list.

    public List<Contact> SellToContacts{ get; set; }
    
    public int SellToContactsCount
    {
        get
        {
             return this.SellToContacts.Count;
        }
    }

    And then in the grid:

    columns.Bound(c => c.SellToContactsCount);

    Another solution is to use a Column Client Template like so:

    columns.Bound(c => c.SellToContacts).ClientTemplate("#= SellToContacts.length #");

    Let me know if you require further assistance.

     

    Best Regards,
    Georgi Denchev
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top