简体   繁体   English

通过字典 <int,Double> 在Controller中查看并访问View中的字典值以显示图表

[英]Passing a Dictionary<int,Double> from Controller to view and access the dictionary values from View to display a chart

I have a controller that Populates a dictionary with key as Month dates as 0,5,10,15,20,25,30 and the Values as some Distance covered for each period. 我有一个控制器,用于填充字典,其中键为月,日期为0,5,10,15,20,25,30,值则为每个期间涵盖的某个距离。 Say for eg: Dictionary element of key 5 has distance covered between days 0 and 5 of that Month. 例如说:键5的词典元素在该月的第0天到第5天之间具有距离。

Dictionary<int, double> ChartDetails = getDetails.getFMS1DataSet(FMS1Resultset);

I need to know how can I pass this dictionary from Controller to View. 我需要知道如何将此字典从Controller传递到View。 In View I access them to display a chart of Number of Days VS distance covered. 在“视图”中,我访问它们以显示“天数VS覆盖距离”图表。 The Chart is SyncFusion User controls 图表是SyncFusion用户控件

@(Html.EJ().Chart("container")
          .PrimaryXAxis(pr => pr.Range(ra => ra.Min(2005).Max(2011).Interval(1)).Title(tl => tl.Text("Days")))
          .PrimaryYAxis(pr => pr.Title(tl => tl.Text("Fuel Usage")).RangePadding(ChartRangePadding.None)
          .LabelFormat("{value}%").Range(ra => ra.Min(25).Max(50).Interval(5)))
          .CommonSeriesOptions(cr => cr.Type(SeriesType.Line).EnableAnimation(true)
          .Marker(mr => mr.Shape(ChartShape.Circle).Size(sz => sz.Height(10).Width(10)).Visible(true)).Border(st => st.Width(2)))
          .Series(sr =>
              {
                  sr.Points(pt =>
                      {
                          pt.X("0").Y(0).Add();
                          pt.X("5").Y().Add();
                          pt.X("10").Y().Add();
                          pt.X("15").Y().Add();
                          pt.X("20").Y().Add();
                          pt.X("25").Y().Add();
                          pt.X("30").Y().Add();
                      }).Name("Fuel").Tooltip(sr1 => sr1.Visible(true).Template("Tooltip")).Add();

              })
          .CanResize(true)

Here for pt.X("5").Y().Add(); 这里是pt.X(“ 5”)。Y()。Add(); I want the Y parameter to be added as Dictionary elements 我希望将Y参数添加为Dictionary元素

It is not possible to directly pass the dictionary values from view to controller as dictionary doesn't allow serialization of data. 由于字典不允许数据序列化,因此无法直接将字典值从视图传递到控制器。 Hence we have created a view model class containing the dictionary type. 因此,我们创建了一个包含字典类型的视图模型类。 The dictionary value is stored in the ViewBag in the controller and passed to the view for assigning values to the series Points. 字典值存储在控制器的ViewBag中,并传递到用于为序列点分配值的视图。 The X value in the chart should be of string type .Hence we have converted the integer Value to string. 图表中的X值应为字符串类型。因此,我们已将整数Value转换为字符串。 Y values are used as double . Y值用作double。 I have attached the sample link for your reference. 我已附上示例链接供您参考。

http://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication11359635681 http://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication11359635681

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM