Home > Back-end >  Support null columns on grid
Support null columns on grid

Time:10-26

I'm currently working on ASP.Net Core MVC app with Telerik Kendo Grid

On the grid I have columns like:

 .Columns(columns =>
                    {
     columns.Bound(x => x.PrimaryContact.EmailAddress)
  })

But this is throwing an error:

Uncaught TypeError: Cannot read properties of null

Because PrimaryContact property can be null

To solve this I try:

columns.Bound(x => x.PrimaryContact != null ? x.PrimaryContact.EmailAddress : string.Empty)

But now is returning the error:

InvalidOperationException: Bound columns require a field or property access expression.

How can I support nullable in kendo columns?

CodePudding user response:

You have to use ClientTemplate:

columns.Bound(x => x.PrimaryContact.EmailAddress)
       .ClientTemplate("#=PrimaryContact ? PrimaryContact.EmailAddress : ''#")

Check: https://feedback.telerik.com/aspnet-mvc/1357413-support-null-complex-properties-in-grid-columns

CodePudding user response:

Bind just PrimaryContact to avoid the null reference in the expression and use a template to define what you actually want the column to display:

.Columns(columns =>
{
    columns.Bound(x => x.PrimaryContact).Template(x =>
    {
        #= x.PrimaryContact != null ? x.PrimaryContact.EmailAddress : string.Empty#
    })
})

This is similar to Dimitris answer (I didn't see his as I typed mine). The difference is that mine is a template rendered server side and his is rendered client side. Does it work any differently?

  • Related