2013-03-05 69 views
0

我想显示一个jqplot饼图,即时能够显示它与静态数据,但我想显示动态数据从数据库..为此,我连接到我的数据库,并将数据绑定到列表,现在如何我可以从我的视角打电话给我吗?将动态数据传递给mvc3中的jqplot饼图?

$(document).ready(function(){ 
var data = [ 
['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14], 
['Out of home', 16],['Commuting', 7], ['Orientation', 9] 
]; 
var plot1 = jQuery.jqplot ('chart1', [data], 
    { 
    seriesDefaults: { 
    // Make this a pie chart. 
    renderer: jQuery.jqplot.PieRenderer, 
    rendererOptions: { 
     // Put data labels on the pie slices. 
     // By default, labels show the percentage of the slice. 
     showDataLabels: true 
    } 
    }, 
    legend: { show:true, location: 'e' } 
    } 
); 
    }); 

,现在我的要求是,以显示从数据库中相同的数据和我在控制器绑定这个数据到列表,现在我怎么能调用列表,从我的观点?

回答

0

考虑到您使用的是MVC,您可以将模型中的所有数据与关键值对列表一起放入,然后将其转换为JSON,同时将其传递给JavaScript。 ,如果你想通过ajax完成,你可以发送请求到json,并从控制器的Action方法获得JSON结果。

使用JavaScript序列化模型为JSON转换成你的操作方法

var data = somedata_with_KeyValuePair; 
var model = new MyModel(); 
var serializer = new JavaScriptSerializer(); 
model.JsonData = serializer.Serialize(data); 
return View(model); 

,然后使用它像这样

var data = <%= Model.JsonData %> 

,或者如果您正在使用MVC 3,那么下面就可以工作,以及

@Html.Raw(Json.Encode(YourObjectContaingListOfKeyValue)) seems to do the trick.