【问题标题】:Kendo UI MVC - How to bind dynamically data of type double[]Kendo UI MVC - 如何动态绑定 double[] 类型的数据
【发布时间】: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,您只能将intstring 绑定到图表列似乎很奇怪...
  • @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


【解决方案1】:

试试像这样的东西

创建模型

 public class ChartsModel
 {       
    public double invoices { get; set; }
   public int Currentweek{ get; set; }
 }

修改你的控制器

public JsonResult _BindInvoiceChart()
    {
        List<InvoiceDto> invoices=your_functionToGetInvoices();
        List<ChartsModel> res = new List<ChartsModel>();    
        var currentWeekNumber = DateHelper.GetWeekOfYear(DateTime.Now);
        var firstWeekNumber = currentWeekNumber - 5;
        for (var i = 0; i < 5; i++)
         {
          ChartsModel chartsModel=new ChartsModel();
          chartsModel.Currentweek=i;
          chartsModel.invoices  = invoices.Where(o =>
            o.InvoiceDate.Year == DateTime.Now.Year &&
            DateHelper.GetWeekOfYear(o.InvoiceDate) == (firstWeekNumber + i)
        ).Sum(o => o.TotalNetCharge);   
        res.Add(chartsModel)
        }   
        return Json(res, JsonRequestBehavior.AllowGet);
    }

查看

@(Html.Kendo().Chart<ChartsModel>()
         .HtmlAttributes(new { style = "width:100%;height:263px" })
  .Name("InvoiceChart")
  .DataSource(dataSource => dataSource
  .Read(read => read.Action("_BindInvoiceChart", "yourCOntrollerName")) // Specify the action method and controller name
  )
  .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(d => d.invoices).Tooltip(tooltip => tooltip.Visible(true)).Name("Week Totals");                    
     // 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}")
    ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多