如何处理 DevExtreme DataGrid 模板中的空数据条件?

How do I handle a null data condition in DevExtreme DataGrid template?

我有以下 class 定义:

public class SubType
{
    [Key] 
    public Guid SubTypeId { get; set; }
    public string SubTypeName { get; set; }
}
public class Item
{
    [Key] 
    public Guid ItemId { get; set; }
    public string Name { get; set; }
    public string Variety { get; set; }
    public SubType ItemSubType { get; set;}
}

这是我的 DevExtreme DataGrid 定义:

@(Html.DevExtreme().DataGrid<Item>()
    .DataSource(Model)
    .Columns(columns => {
        columns.AddFor(m => m.Name);
        columns.AddFor(m => m.Variety);
    })
    .MasterDetail(md => {
        md.Enabled(true);
        md.Template(@<text>
            <div class="master-detail-caption">SubType: <%- data.ItemSubType.SubTypeName%> </div>
        </text>);
    })

当data.ItemSubType为NULL时,出现以下错误:

VM28:3 Uncaught TypeError: Cannot read property 'SubTypeName' of null at eval (eval at (dx.aspnet.mvc.js:75), :3:145) at Object.render (dx.aspnet.mvc.js:85) at t.n._renderCore (dx.all.js:80) at t.render (dx.all.js:15) at t._renderDelayedTemplatesCore (dx.all.js:42) at t.renderDelayedTemplates (dx.all.js:42) at t.renderDelayedTemplates (dx.all.js:107) at t.renderDelayedTemplates (dx.all.js:10) at t.renderDelayedTemplates (dx.all.js:112) at t.renderDelayedTemplates (dx.all.js:10)

当Data.ItemSubType不为空时,SubTypeName显示无误。

如何处理ItemSubType 为空的情况? 有没有办法在默认 string/value 为 null 时有条件地显示它?

使用条件运算符a ? b : c

<%- data.ItemSubType != null? data.ItemSubType.SubTypeName : "NoSubType"%>