如何从JSON请求中检索值?

时间:2016-09-20 05:05:42

标签: json razor telerik pie-chart

所以我使用Telerik使用以下代码实现图表

 @(Html.Kendo().Chart<OCM.Model.Dashboard>()
                        .Name("chart")
                        .Title(title => title
                                .Text("Customer Satisfaction")
                                        .Position(ChartTitlePosition.Bottom))
                                .Legend(legend => legend
                                .Visible(false)
                        )
                        .ChartArea(chart => chart
                            .Background("transparent")
                        )
                        .HtmlAttributes(new { style = "background: center no-repeat url(" + @Url.Content("~/Content/shared/world-map.png") })
                        .Series(series =>
                        {
                            series.Pie(new dynamic[] {
                                new {category="Happy",color="#9de219"},
                                new {category="Unhappy",color="#90cc38"},
                                new {category="Normal",color="#068c35"}
                            })
                            .Labels(labels => labels
                                .Template("#= category #: \n #= value#%")
                                .Background("transparent")
                                .Visible(true)
                            )
                            .StartAngle(150);
                        })
                        .DataSource(ds =>
                        {
                            ds.Read(read => read.Action("GetSatisfaction", "Dashboards"));
                        })
                        .Tooltip(tooltip => tooltip
                            .Visible(true)
                            .Format("{0}%")
                        )
                    )

我正在通过控制器使用JSON请求从数据库中获取值。

public ActionResult GetSatisfaction([DataSourceRequest] DataSourceRequest request)
    {
        return Json(Data().ToDataSourceResult(request));

    }

    private List<DashboardViewModel> Data()
    {
        var details = new List<DashboardViewModel>();

        List<Dashboard> social = BALDashboard.GetSocialMediaDashboard();

        Mapper.CreateMap<Dashboard, DashboardViewModel>();

        return Mapper.Map<List<DashboardViewModel>>(social);


    }

现在JSON请求正常,我可以从数据库中检索值。我的问题是我想将这些值传递给Happy,Unhappy和Normal的图表,以便显示饼图值,但是因为我猜JSON以数组的形式传递它不起作用。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

所以我通过使用ViewBag解决了这个问题并取消了JSON请求

        ViewBag.Happy = 2;
        ViewBag.UnHappy = 2;
        ViewBag.Normal = 2;
        List<Dashboard> social = BALDashboard.GetSocialMediaDashboard();

        Mapper.CreateMap<Dashboard, DashboardViewModel>();

        //if(social == null)
        //    social = new Dashboard();

        ViewData["social"] = social;

        ViewBag.Happy = social[0].Happy ;
        ViewBag.UnHappy = social[0].UnHappy;
        ViewBag.Normal = social[0].Normal;
        return View();