Using Nested Model Properties
Environment
Product | ASP.NET MVC Grid |
Operating System | All |
Browser | All |
Browser Version | All |
Description
How can I use nested Model properties in the ASP.NET MVC'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-mvc/api/kendo.mvc.ui.fluent/customdatasourcemodelfielddescriptorbuilder#fromsystemstring)
configuration property of the [schema.model.field](https://docs.telerik.com/aspnet-mvc/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");
})
)
)