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.

dovid
  • 6,354
  • 3
  • 33
  • 73
  • 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 expression – hajirazin Jun 10 '15 at 06:24

2 Answers2

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/

mijail
  • 385
  • 1
  • 5
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);
Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38