New to Telerik UI for ASP.NET Core? Download free 30-day trial

Use Nested Model Properties

Environment

Product ASP.NET Core Grid
Operating System All
Browser All
Browser Version All

Description

How can I use nested Model properties in the ASP.NET Core's Grid?

Solution

To bind a column of the Grid to a Model with nested properties you need to use a Custom DataSource which allows you to configure the [From](https://docs.telerik.com/aspnet-core/api/Kendo.Mvc.UI.Fluent/CustomDataSourceModelFieldDescriptorBuilder#fromsystemstring) configuration property of the [Schema.Model.Field](https://docs.telerik.com/aspnet-core/api/Kendo.Mvc.UI.Fluent/CustomDataSourceModelDescriptorFactory#fieldsystemstring).

In addition when you use From together with CRUD operations and add new rows, you also have to define in Schema.Model.Field the original field or the sequence of nested fields which are used inside From.

The reason is that during updates and creates, the Telerik UI DataSource tries to construct a data item object which matches the original (server-side) data-item structure. For new data items, such a structure does not exist and needs to be defined explicitly.

  ...
  .DataSource(ds => ds
              .Custom()
              .PageSize(20)
              .ServerFiltering(false)
              .Schema(schema =>
                  schema.Model(model =>
                  {
                    model.Id(id => id.Name);
                    model.Field(f => f.Name);
                    model.Field(f => f.Description).From("Description.Text");
                }).Data("Data").Total("Total")
            )
            .Transport(transport =>
            {
                transport.Read("Orders_Read", "Grid");
                transport.Update("Orders_Update", "Grid");
                transport.Create("Orders_Create", "Grid");
                transport.Destroy("Orders_Destroy", "Grid");
            })
            )
    )

See Also

In this article