Using data annotations when dynamic specifying data model

5 posts, 0 answers
  1. Aleks
    Aleks avatar
    23 posts
    Member since:
    Nov 2018

    Posted 17 Feb 2020 Link to this post

    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?

  2. Preslav
    Admin
    Preslav avatar
    591 posts

    Posted 20 Feb 2020 Link to this post

    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.
  3. Aleks
    Aleks avatar
    23 posts
    Member since:
    Nov 2018

    Posted 22 Feb 2020 Link to this post

    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

  4. Preslav
    Admin
    Preslav avatar
    591 posts

    Posted 26 Feb 2020 Link to this post

    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.
  5. Aleks
    Aleks avatar
    23 posts
    Member since:
    Nov 2018

    Posted 26 Feb 2020 in reply to Preslav Link to this post

    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.
Back to Top