4

I am trying using Telerik MVC with a collection of dynamics ExpandoObjects.

The Controller is:

[GridAction]
public ActionResult TestDiario()
{
        var result = new List<dynamic>();

        dynamic diarioModel = new ExpandoObject();

        var dictionary = (IDictionary<string, object>)diarioModel;

        dictionary.Add("ID", 1);
        dictionary.Add("AlunoID", 12781);
        dictionary.Add("DAY05CLASS1", true);
        dictionary.Add("DAY05CLASS2", true);
        dictionary.Add("DAY07CLASS1", true);
        dictionary.Add("DAY08CLASS1", true);

        result.Add(diarioModel);
        return View(result);
}

The View is:

@using Telerik.Web.Mvc.UI

@model IEnumerable<dynamic>

@{
    ViewBag.Title = "TestDiario";
}

@(Html.Telerik().Grid(Model).Name("Grid")
    .DataKeys(dataKeys => dataKeys.Add("ID"))
    .Columns(columns => 
    { 
        columns.Bound("MatAnoID").Visible(true);
        columns.Bound("AlunoID");
        columns.Bound("NroClasse");
        columns.Bound("Aluno");

        var dictionary = (IDictionary<string, object>)Model;
        foreach (var property in (IDictionary<String, Object>)dictionary)
        {
            if (property.Key.ToString().Remove(3) == "DAY")
            {
                columns.Bound(property.Key);
            }
        }
    })
    .Pageable()
    .Sortable()
    .Groupable()
    .Filterable()

)

The loop foreach gets the dynamics fields started by DAY string.

When I run the Project the follow error appear:

{"Is not possible convert an typed object 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.Generic.IDictionary2[System.String,System.Object]'."}

Is there a way to use dynamic object with Telerik MVC Control with loop through fields?

Pierre
  • 488
  • 4
  • 20
user1822331
  • 49
  • 1
  • 4

1 Answers1

3

Yes you can, and you were fairly close but made a few mistakes.

1) Model is not of type IDictionary because it is not an expando object. It is a list of dynamic.

This is a bit of a hack but to solve that problem I just took the first (or default) element in the enumerable and then made a dictionary out of that.

2) You are trying to bind columns which do not exist as properties in your expando object.

I commented those out.

3) I think you were looking for keys that start with "DAY". If you want to remove "DAY" from the column name you can tweak my example code.

Other than that it works fine:

@(Html.Telerik().Grid(Model).Name("Grid")
    .DataKeys(dataKeys => dataKeys.Add("ID"))
    .Columns(columns => 
    { 
        //NOTE: some of these columns are not valid because you didn't include them as properties
        //columns.Bound("MatAnoID").Visible(true);
        //columns.Bound("NroClasse");
        //columns.Bound("Aluno");

        columns.Bound("AlunoID");

        var first = Model.FirstOrDefault();
        if (first != null) {
            var dictionary = (IDictionary<string, object>)first;
            foreach (var property in dictionary) {
                string key = property.Key.ToString();
                if (key.StartsWith("day", StringComparison.InvariantCultureIgnoreCase)) {
                    columns.Bound(property.Key);
                }
            }
        }

    })
    .Pageable()
    .Sortable()
    .Groupable()
    .Filterable()
)
Sean Chase
  • 1,139
  • 1
  • 15
  • 23