This is a migrated thread and some comments may be shown as answers.

Using data annotations when dynamic specifying data model

4 Answers 495 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aleks
Top achievements
Rank 1
Veteran
Aleks asked on 18 Feb 2020, 01:22 AM

I'm trying to populate a grid dynamically as I have a number of different report data models that need to be displayed in the grid depending on user selection.

Each data model inherits from a common interface, and I'm passing an `IEnumerable` of that interface through to the view.

Generally this works, and the data is displayed, however, the `DisplayAttribute` of the source model is ignored.

If I supply an `IEnumerable` of the model itself, this works, however, as the view needs to support dynamically specified models, I cannot do this.

I have tried the `LoadSettings` method shown in the grid demo, however, I get an error because the `Member` property specified cannot be found on the interface.

How can I dynamically specify the model to use and have the grid honour the data annotations of the model?

4 Answers, 1 is accepted

Sort by
0
Preslav
Telerik team
answered on 20 Feb 2020, 01:52 PM

Hello Aleks,

Based on your post, I believe that using a dynamic model for the Grid and binding it to a DataTable can work for you.

For example, I am attaching a sample ASP.NET Core project demonstrating the above.

 

Regards,
Preslav
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
0
Aleks
Top achievements
Rank 1
Veteran
answered on 22 Feb 2020, 10:47 AM

Hi Preslav,

Your example seems to be pulling the data twice... once in

public IActionResult Index()

and then again in

public IActionResult Customers_Read([DataSourceRequest] DataSourceRequest request)

Is there any way to get this working with local data only?

If I omit the remote "Read" in the DataSource section, I get the columns but no data.

I cannot pass the model to the Grid constructor, because it expects it to be IEnumerable

0
Preslav
Telerik team
answered on 26 Feb 2020, 03:03 PM

Hello Aleks,

I understand that this behavior could be a performance issue. However, I see no other way of achieving the desired outcome as you need the model in order to create the columns, and then you need to read and bind the data. As you said, you cannot pass the model to the Grid as it expects it to be IEnumerable.

 

Regards,
Preslav
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
0
Aleks
Top achievements
Rank 1
Veteran
answered on 26 Feb 2020, 08:27 PM
You don't need the model to create the columns though!

This works fine:- (apologies, code formatting in the forums is still broken)

@model IEnumerable<IReport>

@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Pageable()
    .Sortable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .ServerOperation(false)
    )
)

IReport is just:

public interface IReport { }

And Book might be something like this:-

public class BookModel : IReport
{
    [Display(Name = "Book Name")]
    public Guid BookName { get; set; }

    //...
}

If I do the above, it displays the columns and data, however, the title of the column is "BookName" instead of "Book Name".

If I do this:-

@model IEnumerable<BookModel>

@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Pageable()
    .Sortable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .ServerOperation(false)
    )
)

Then the column title is "Book Name" as expected.

You're obviously unboxing from IReport to generate the columns in the first example... it just doesn't honour the Display attributes.
Tags
Grid
Asked by
Aleks
Top achievements
Rank 1
Veteran
Answers by
Preslav
Telerik team
Aleks
Top achievements
Rank 1
Veteran
Share this question
or