Telerik mvc grid, columns.bound to dictionary value. or & ldquo; Dynamic Property in Model & rdquo;

advertisements

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.


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/