我如何将值从C#传递到客户端jQuery
问题描述:
嗨
我想使用Jquery在asp.net中创建图表,但数据来自服务器端c#和Sql Server
HTML代码
Hi
I want to create a charts in asp.net using Jquery but the data comes from server side c# and Sql Server
Html Code
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer",
{
theme: "theme2",
title:{
text: "Earthquakes - per month"
},
animationEnabled: true,
axisX: {
valueFormatString: "MMM",
interval:1,
intervalType: "month"
},
axisY:{
includeZero: false
},
data: [
{
type: "line",
//lineThickness: 3,
dataPoints: [
{ x: new Date(2012, 00, 1), y: 450 },
{ x: new Date(2012, 01, 1), y: 414},
{ x: new Date(2012, 02, 1), y: 520, indexLabel: "highest",markerColor: "red", markerType: "triangle"},
{ x: new Date(2012, 03, 1), y: 460 },
{ x: new Date(2012, 04, 1), y: 450 },
{ x: new Date(2012, 05, 1), y: 500 },
{ x: new Date(2012, 06, 1), y: 480 },
{ x: new Date(2012, 07, 1), y: 480 },
{ x: new Date(2012, 08, 1), y: 410 , indexLabel: "lowest",markerColor: "DarkSlateGrey", markerType: "cross"},
{ x: new Date(2012, 09, 1), y: 500 },
{ x: new Date(2012, 10, 1), y: 480 },
{ x: new Date(2012, 11, 1), y: 510 }
]
}
]
});
chart.render();
}
</script>
<script type="text/javascript" src="/assets/script/canvasjs.min.js"></script></head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</body>
</html>
现在,我想从数据库传递x轴和Y轴值.请帮助我
Now I want to pass x-axis and Y-axis value from database . Please Help me
答
,当您的视图加载从数据库中获取x和y轴值并将其放在Viewbag元素中.然后,您可以在jquery中轻松访问Viewbag
when your view loads get the x and y axis value from database and put that inside a Viewbag element.Then Viewbag you can easily access in jquery