Telerik mvc grid, columns.bound to dictionary value. or "dynamic property in model"
Asked Answered
H

2

7

i have a model with dynamic "propertys" (in DB level, something similar to Entity-Attribute-Value system). The properties are in a field of Dictionary<int, string>, and would like to show them into columns.

Model:

the model have a static array (initalized in static counstractor) of "property" names & keys:

 public static ModelSpecialParameter[] SpecialFields;

and a Dictionary (initalized in model create, and all posible keys added) with their values.

public Dictionary<int, string> ValuesForDynamicProps;

The View:

@(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
    //other columns for realy propertry
    //columns.Bound(e => ...
    //columns.Bound(e => ...

    foreach (var  item in SpecialFields) // SpecialFields is a static collection. represent the name an id of the dynamic property's.
    {
        columns.Bound("ValuesForDynamicProps[" + item.IdProperty + "]").Width(140).Title(item.DisplayName);
    }
}

I get an error:

{"Bound columns require a field or property access expression."}

i tryed also:

columns.Bound(e => e.ValuesForDynamicProps[item.IdProperty]).Width(140).Title(item.DisplayName);

the same error.

Even if what I want is not possible I am looking for an idea how to get the desired result: Flexibility in adding and removing properties.

Hyaloid answered 4/6, 2015 at 17:24 Comment(1)
I think e => e.ValuesForDynamicProps[item.IdProperty] is not a type of PropertyExpression. for that you need to create one static method that returns property expression. No need of lambda here as it is getting converted to Property Expression. you can directly pass the expressionCaltrop
L
1

There isn't an easy way to do directly with Fluent API. What you have to do it's an ajax call from another method that will return a JSON with that structured data all in one.

Or try this other way, it's almost the same idea you were trying. Maybe can help you: https://mycodepad.wordpress.com/2014/12/01/asp-net-mvc-5-with-razor-kendo-ui-dynamic-grid-creation-columns-ordering-grouping-and-paging/

Licastro answered 4/6, 2015 at 18:15 Comment(0)
B
0

As far as i understand, you have problem here:

columns.Bound(e => ValuesForDynamicProps[item.IdProperty]).Width(140).Title(item.DisplayName);

You pass to Bound() method lambda but not use its parameter (model). There should be somethin like:

columns.Bound(e => e.ValuesForDynamicProps[item.IdProperty]).Width(140).Title(item.DisplayName);
Bik answered 7/6, 2015 at 10:34 Comment(1)
thank, its just a type mistake in the question. in the source its correct with e..Hyaloid

© 2022 - 2024 — McMap. All rights reserved.