Completed
Last Updated: 21 Jan 2021 08:09 by ADMIN
Nick
Created on: 10 Dec 2012 11:36
Category: UI for ASP.NET MVC
Type: Feature Request
9
Support null complex properties in grid columns
c.Bound( o => o.Complex.Property )

This throws a js exception when o.Complex is null. The grid fails to render when certain data is bound.

c.Bound( o => o.Complex == null ? null : o.Complex.Property )

This throws a .NET InvalidOperationException : "Bound columns require a field or property access expression"
1 comment
ADMIN
Alex Hajigeorgieva
Posted on: 21 Jan 2021 08:09

Hello, Nick,

The JavaScript error is expected since we are trying to access a property on an undefined object, however, the server error you mentioned is not reproducible with our latest version 2021.1.119. Here is the test scenario I used:

Grid view

  @(Html.Kendo().Grid<MVCGrid.Models.OrderViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Freight);
        columns.Bound(p => p.ShipName);
        columns.Bound(p => p.OrderDate);
        columns.Bound(p => p.Supplier.Name).ClientTemplate("#=Supplier? Supplier.Name: 'N/A'#");
    })
    .HtmlAttributes(new { style = "height:550px;" })
    .DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(10)
    .Read(read => read.Action("Orders_Read", "Grid"))
    )
)

Models

public class OrderViewModel
{

        public decimal? Freight
        {
            get;
            set;
        }

        [Required]
        public DateTime? OrderDate
        {
            get;
            set;
        }

        public string ShipName
        {
            get;
            set;
        }

        public Supplier Supplier
        {
            get;
            set;
        }
    }
// complex object class
public class Supplier
    {
        public int Id { get; set; }

        public string Name { get; set; }
    }
Controller

       public static List<Supplier> suppliers = Enumerable.Range(1, 10).Select(i =>
            new Supplier() { Id = i, Name = "Name " + i }
        ).ToList();

        public static List<OrderViewModel> orders = Enumerable.Range(1, 10).Select(i => new OrderViewModel
        {
Freight = i * 10, OrderDate = DateTime.Now.AddDays(i), ShipName = "ShipName " + i, Supplier = i % 3 == 0? suppliers.ElementAt(5): null }).ToList(); public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request) { return Json(orders.ToDataSourceResult(request)); }
Resulting Grid - The last column shows the nested object

Kind Regards,
Alex Hajigeorgieva
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/.