简体   繁体   中英

How to check the null values in Telerik grid

I want to check the null values in Telerik grid. Because I want to show the first 10 characters.

if "Address" is null it getting an error and it says
Object reference not set to an instance of an object

This is my code.

     @(Html.Telerik().Grid(Model)
                      .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(o => o.PKID).Title("ID");

            columns.Bound(o => o.PKID)
.Width(50)
   .Template(o => o.Address.Substring(0, 10)+ "...").Title("TO");// Eroor is hers      
        })
          .Pageable(paging => paging.PageSize(15).Style(GridPagerStyles.NextPreviousAndNumeric).Position(GridPagerPosition.Bottom))
                 .Sortable()
    ) 

First, Telerik (or Kendo) Grid doesn't support well a nested properties. It's better to avoid such properties in a model.

Second, you need to check for non null value and address length in a Template method call:

.Template(o => o.Address != null && o.Address.Length > 10 ? 
    o.Address.Substring(0, 10) + "..." : o.Address).Title("TO")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM