剑道饼图MVC数据绑定

时间:2014-12-10 16:46:32

标签: kendo-ui kendo-asp.net-mvc kendo-dataviz kendo-chart

我在MVC中遇到Telerik的Kendo UI饼图问题。我在他们的网站上找不到任何合适的代码示例。我发现的一个演示只显示了一半的代码,所以我试着猜测如何让它工作。我收到了错误,无论我试图消除错误,似乎没有任何效果。它实际上绑定数据的series.Pie部分有问题。我复制了他们示例中的代码,并使用了lambda表达式,就像他们一样。 model => model.Percentage and model => model.StatusName用于我的视图模型。但它在第一个lambda上出错,并出现以下错误信息:

CS1660:无法将lambda表达式转换为' string'因为它不是委托类型

以下是相关代码:

ViewModel / View:

public class StatusPercentageViewModel
{
    public string StatusName { get; set; }
    public int Count { get; set; }
    public double Percentage { get; set; }
}


@model IEnumerable<StatusPercentageViewModel>
@(Html.Kendo().Chart(Model)
    .Name("StatusBreakdown")
    .Legend(legend => legend
        .Position(ChartLegendPosition.Bottom)
    )
    .Series(series =>
    {
        series.Pie(
            model => model.Percentage,
            model => model.StatusName,
            null,
            null
        );
    })
    .Tooltip(tooltip => tooltip.
        Template("${ category } - ${ value }%").Visible(true)
    )
)

1 个答案:

答案 0 :(得分:0)

如果要像这样定义系列,可能需要指定模型。见this example

例如:

@(Html.Kendo().Chart<StatusPercentageViewModel>()
    .Name("StatusBreakdown")
    .Legend(legend => legend.Position(ChartLegendPosition.Bottom))
    .DataSource(ds => ds.Read(read => read.Action("GetStatus", "Status")))
    .Series(series =>
    {
        series.Pie(
            model => model.Percentage,
            model => model.StatusName
        );
    })
    .Tooltip(tooltip => tooltip.Template("${ category } - ${ value }%").Visible(true))
)

另一种方法(更类似于你现在拥有的)将是:

@model IEnumerable<StatusPercentageViewModel>
@(Html.Kendo().Chart()
    .Name("StatusBreakdown")
    .Legend(legend => legend.Position(ChartLegendPosition.Bottom))
    .Series(series => series.Pie(Model))
    .Tooltip(tooltip => tooltip.Template("${ category } - ${ value }%").Visible(true))
)