【发布时间】:2017-06-19 14:53:57
【问题描述】:
我在尝试将列绑定到 Kendo UI MVC 图表时遇到以下错误:
InvalidOperationException:绑定列需要字段或属性访问表达式。
<div class="demo-section k-content wide">
@(Html.Kendo().Chart(Model)
.Name("chart")
.Theme("blueOpal")
.HtmlAttributes(new { style = "height: 400px;" })
.Title("Site Visitors Stats /thousands/")
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.SeriesDefaults(seriesDefaults => seriesDefaults
.Column().Stack(true)
)
.Series(series =>
{
series.Column(model => ChartDataHelper.GetWeekTotals(model.Invoices)).Name("Week Totals"); // fails
series.Column(new double[] { 52000, 34000, 23000, 48000, 67000, 83000 }).Name("Unique visitors"); // works
})
.CategoryAxis(axis => axis
.Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun")
.MajorGridLines(lines => lines.Visible(false))
)
.ValueAxis(axis => axis
.Numeric()
.Line(line => line.Visible(false))
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0}")
)
)
</div>
这里是辅助静态类的代码:
public static class ChartDataHelper
{
public static decimal[] GetWeekTotals(ICollection<InvoiceDto> invoices)
{
var currentWeekNumber = DateHelper.GetWeekOfYear(DateTime.Now);
var firstWeekNumber = currentWeekNumber - 5;
decimal[] results = new decimal[5];
for (var i = 0; i < 5; i++)
{
results[i] = invoices.Where(o =>
o.InvoiceDate.Year == DateTime.Now.Year &&
DateHelper.GetWeekOfYear(o.InvoiceDate) == (firstWeekNumber + i)
).Sum(o => o.TotalNetCharge);
}
return results;
}
似乎由于decimal[] 不是原始类型,因此动态传递数据似乎存在问题,但是在创建double[] 类型的匿名对象时,如第二系列专栏所示,没有问题。怎么样?
【问题讨论】:
-
只是一个想法,但不是使用静态帮助器类来生成每周总计的数组,而是可以向模型添加一个字段/属性以公开有关发票的相同聚合信息总计?
-
不确定这是否是问题的原因,但您的
GetWeekTotals函数实例化了一个长度为 5 的decimal数组,但您的循环仅将值分配给位置 reference material -
@NeilHibbert,您只能将
int或string绑定到图表列似乎很奇怪... -
@JFBeaulieu 你确定这是正确的吗?错误消息仅暗示“绑定列需要字段或属性访问表达式”它没有提到仅涉及 int 或字符串...?
-
@NeilHibbert,我已经尝试公开成员类型
decimal[]但这会产生此错误:InvalidOperationException: The property 'WeeksTotals' could not be mapped, because it is of type 'Decimal[]' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it.
标签: c# asp.net-mvc kendo-ui kendo-asp.net-mvc