Column binds automatically the name of JsonPropertyName Attribute

1 Answer 44 Views
Grid
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Marco asked on 28 Nov 2024, 11:34 AM

Hy,

I am trying to display a list of data from an api model inside a TelerikGrid.

The api model is automatically generated and looks like this:

Data binding is done via a handler method in the razor page that return a JsonResult of the data list. 

In this example below the column bind the property name "SalesGroup", but the bound method  automatically resolves the name "salesGroup" (the name of JsonPropertyName attribute). Since the first letter is lowercase, the column does not display the value

I have currently created an extension of the Bound() method that resolves the correct name based on whether the "JsonPropertyName" attribute is present or not.

I would like to avoid using the "Bound(string memberName)" method. 

Is there already a solution to my problem?

Thanks

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Petar
Telerik team
answered on 03 Dec 2024, 08:49 AM

Hello Marco,

Thank you for providing the details.

The solution you implemented using the Bound method with the extension is a valid choice. If you are looking for an alternative solution, I can suggest relying on a property name expression instead of the one defined by the JsonPropertyName attribute. You can achieve this by using a Custom DataSource, where you can define how the mapping will be handled through the model by utilizing the From option. Below is an example of how to use this feature:

@(Html.Kendo().Grid<ProductViewModel>()
    .Name("Grid")
    .Columns(columns => {
        columns.Bound(p => p.ProductName);
        columns.Bound(p => p.UnitPrice).Width(140);
        columns.Bound(p => p.Discontinued).Width(100);
    })
// ...
    .DataSource(dataSource => dataSource
        .Custom()
        .Batch(true)
        .PageSize(20)
        .Schema(schema => schema.Model(model =>
        {
            model.Id(p => p.ProductID);
            model.Field(p => p.ProductID);
            model.Field(p => p.ProductName).From("productName");
            model.Field(p => p.UnitPrice);
            model.Field(p => p.Discontinued);
        }))
        .Transport(transport =>
        {
            transport.Read(r => r.Url("/Index?handler=Read").Data("forgeryToken"));
        })
    )
)

I hope this helps. Please feel free to reach out if you have any further concerns or questions.

Regards,
Petar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Petar
Telerik team
Share this question
or