MVC Core Grid Model Binder .From method no longer exists, what to use instead?

0 Answers 87 Views
Grid
Max
Top achievements
Rank 1
Max asked on 26 Feb 2023, 08:12 PM | edited on 26 Feb 2023, 08:37 PM

The model I have to work with has mixed Json property names. Can“t easiely unify them as that will break filtering on the db. While the grid has a seperate model from the db entity ToDataSourceResultAsync is called after mapping the IQueryable, so cosmosdb will use the json attributes on the grid model and not the db entity to generate the query. Prior to .net core there supposedly was a method to define the field name .From.

.Model(model =>
{
   model.Field(p => p.Name).From("name");
})

The .From method no longer seems to exist. My workaround for now was to implement a custom extension method that uses reflection to set the name.

public static class KendoExtensions
{
    public static DataSourceModelFieldDescriptorBuilderBase<T, TDataSourceModelFieldDescriptorBuilder> MapFrom<T, TDataSourceModelFieldDescriptorBuilder>(
        this DataSourceModelFieldDescriptorBuilderBase<T, TDataSourceModelFieldDescriptorBuilder> builder, string name)
        where TDataSourceModelFieldDescriptorBuilder : DataSourceModelFieldDescriptorBuilderBase<T, TDataSourceModelFieldDescriptorBuilder>
    {
        var fieldInfo = builder.GetType()
            .GetField("descriptor", BindingFlags.Instance | BindingFlags.NonPublic);

        var descriptor = (ModelFieldDescriptor)fieldInfo!.GetValue(builder);

        descriptor!.From = name;
        return builder;
    }
}

Is there another way of doing this now? The above works just fine right now, but accessing protected members of 3rd party libraries is something I would very much like to avoid.

The following is the link where they said that there is supposed to be a .From method, but that does not seem to exist for me in 2023.1.117.

https://www.telerik.com/forums/json-serialization-breaks-grid

___

Only just now saw that the last reply said that this is only available when using a Custom Data Source. Guess I gotta do that instead then. Will try that tomorrow. Not sure why that would be required looks like a case of more boilerplate for no reason to me.

 

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Max
Top achievements
Rank 1
Share this question
or