我正在尝试使用Telerik MVC与动态ExpandoObjects集合。
控制器代码如下:
[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);
}
视图是:
@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()
)
foreach循环获取以DAY字符串开头的动态字段。
当我运行项目时,出现以下错误:
{"无法将一个类型化对象'System.Collections.Generic.List1[System.Object]'转换为类型'System.Collections.Generic.IDictionary
2[System.String,System.Object]'。"}
是否有一种方法可以在使用Telerik MVC控件时使用动态对象来循环遍历字段?