質問

I am using the Infragistics Ignite UI grid in my ASP.NET MVC 4 application. Here's what it looks like in the view:

@model IQueryable<User>

@( Html.Infragistics().Grid( Model )
       .AutoGenerateColumns( false )
       .Columns( column => {
                     column.For( p => p.Id)
                           .HeaderText( "ID" );
                     column.For( p => p.Name )
                           .HeaderText( "Name" );
                 } )
       .DataSourceUrl( Html.BuildUrlFromExpression<UserController>( c => c.GetUsers() ) )
       .Features( feature => {
                      feature.Paging().Type( OpType.Remote );
                      feature.Sorting().Type( OpType.Remote ).CaseSensitive( false );
                      feature.Filtering().Type( OpType.Remote );
                      feature.Resizing();
                      feature.Tooltips();
                  } )
       .Width( "100%" )
       .DataBind()
       .Render() )

One of the properties of the User object is a child entity that can be null. Here's the gist of the schema:

public class User
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual Role Role { get; set; }
}

public class Role
{
    public virtual int Id { get; set; }

    public virtual string Description { get; set; }
}

The grid is working great but I need to display the Description property of the Role child in a column. This seems like it should be easy enough but I haven't been able to figure it out. For instance, this doesn't work because Role is null sometimes (it shows the header row of the grid and a perpetual loading icon):

.Columns( column => {
              column.For( p => p.Id)
                    .HeaderText( "ID" );
              column.For( p => p.Name )
                    .HeaderText( "Name" );
              column.For( p => p.Role.Description )
                    .HeaderText( "Role" );
          } )

This doesn't work either:

column.For( p => p.Role.Description )
      .HeaderText( "Role" )
      .Template( "${Site.Name}" );
役に立ちましたか?

解決

I looked high and low for "Infragistics grid nullable property" but didn't find anything until I searched for "Ignite UI grid nullable property". This works:

column.For( p => p.Role )
      .HeaderText( "Role" )
      .DataType( "object" )
      .FormatterFunction( "function(obj) { if (!obj) return 'None'; return obj.Description; }" );

I hate it when that happens. (source)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top