Kendo MVC Grid ICollection Count

1 posts, 0 answers
  1. Chad England
    Chad England avatar
    4 posts
    Member since:
    Mar 2006

    Posted 01 Aug 2012 Link to this post

    Using MVC and Entity Framework I have a model that looks like this:

     public class AddressType
        {
            [ScaffoldColumn(false)]
            public int AddressTypeId { get; set; }
            [Display(Name = "Address Type", Prompt = "Select an Address Type")]
            [Required(ErrorMessage = "Please Select an Address Type.")]
            public string AddressTypeName { get; set; }
            [Display(Name = "Order", Prompt = "Select a display order")]
            [Required(ErrorMessage = "Please Select a display order.")]
            [Range(0, 10000)]
            public int DisplayOrder { get; set; }
            public DateTime? DateCreated { get; set; }
            public string CreatedBy { get; set; }
            public DateTime? DateModified { get; set; }
            public string ModifiedBy { get; set; }
            public byte[] RowVersion { get; set; }
            [Display(Name = "Address")]
            public virtual ICollection<Address> Addresses { get; set; }
        }

    Normally in MVC I can do the following
    <td>
    @(item.Addresses == null ? "None" : item.Addresses.Count.ToString())
    </td>


    Currently my Grid code looks like this, so far I am having no luck getting the count to display for Addresses

    @(Html.Kendo().Grid(Model)    
        .Name("Grid")
        .Columns(columns => {
            columns.Bound(p => p.AddressTypeName);
            columns.Bound(p => p.DisplayOrder);
            columns.Bound(p => p.Addresses);
            columns.Command(command => { command.Edit(); command.Destroy(); });
        })
        .ToolBar(toolbar => toolbar.Create())
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .Pageable()
        .Sortable()
        .Scrollable()
        .DataSource(dataSource => dataSource
            .Server()
            .PageSize(5)
            .Model(model => {
                model.Id(p => p.AddressTypeId);
                model.Field(p => p.Addresses).Editable(false);
            })
            .Aggregates(aggregates =>
        {
            aggregates.Add(p => p.Addresses).Count().ToString();
        })
            .Read("Index", "AddressType")
            .Update("Edit", "AddressType")
            .Create("Create", "AddressType")
            .Destroy("Delete", "AddressType")
        )
    )
Back to Top