2016-09-20 134 views
0

所以我使用Telerik的使用下面的代碼如何從JSON請求中檢索值?

@(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請求可以工作,我可以從數據庫檢索值。我的問題是我想要將這些值傳遞給圖表中的快樂,不快樂和正常,以便餅圖值顯示,但由於我猜JSON以數組的形式傳遞它不起作用。

任何人都可以幫我嗎?

+0

請注意,模型 - 視圖 - 控制器標記是關於模式的問題。 ASP.NET-MVC實現有一個特定的標籤。 –

回答

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();