我有一个具有动态“属性”的模型(在数据库级别,类似于 Entity-Attribute-Value 系统)。
这些属性位于 Dictionary<int, string>
字段中,并希望将它们显示到列中。
型号:
模型有一个包含“属性”名称和键的静态数组(在静态构造器中初始化):
public static ModelSpecialParameter[] SpecialFields;
和一个字典(在模型创建中初始化,并添加所有可能的键)及其值。
public Dictionary<int, string> ValuesForDynamicProps;
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);
}
}
我收到错误:
{"Bound columns require a field or property access expression."}
我也尝试过:
columns.Bound(e => e.ValuesForDynamicProps[item.IdProperty]).Width(140).Title(item.DisplayName);
同样的错误。
即使我想要的不可能,我也在寻找如何获得所需结果的想法: 灵活地添加和删除属性。
最佳答案
没有一种简单的方法可以直接使用 Fluent API。您所要做的就是从另一个方法进行 ajax 调用,该方法将返回一个 JSON,其中包含所有结构化数据。
或者尝试其他方式,这几乎与您尝试的想法相同。也许可以帮助你: https://mycodepad.wordpress.com/2014/12/01/asp-net-mvc-5-with-razor-kendo-ui-dynamic-grid-creation-columns-ordering-grouping-and-paging/
关于asp.net-mvc - Telerik mvc 网格,columns.bound 到字典值。或 "dynamic property in model",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650326/