我正在尝试将jqplot与mvc4一起使用。 '我试图在控制器中获取绘图数据,如下所示:
Controller.cs
public ActionResult DrawChart(FormCollection fc)
{
if (fc["start"] != null && (fc["end"] != null))
{
string start = fc.GetValue("start").AttemptedValue;
string end = fc.GetValue("end").AttemptedValue;
string connstring = "Data Source=DB.world;User ID=USER;Password=mypass;";
using (OracleConnection conn = new OracleConnection(connstring))
{
conn.Open();
string sql = "select to_char(dpr_ts, 'DD.MM.YYYY'), dpr_close from dpr@price where dpr_qot_id=2029543939 and dpr_ts>='" + start + "' and dpr_ts<='" + end + "'";
//"select dpr_close, dpr_ts from dpr@price where dpr_qot_id=2029543939";// and dpr_ts>='01.08.2012' and dpr_ts<='15.11.2012'";
using (OracleCommand comm = new OracleCommand(sql, conn))
{
using (OracleDataReader rdr = comm.ExecuteReader())
{
var arr1 = new object[1];
var arr2 = new object[rdr.RowSize];
var ctr = 0;
while (rdr.Read())
{
arr2[ctr++] = new object[] { rdr.GetOracleString(0).Value, rdr.GetOracleDecimal(1).Value,};
arr1[0] = arr2;
}
var json = new JavaScriptSerializer().Serialize(arr1);
//Json(arr1);
ViewBag.ChartData = json;
Console.WriteLine(ViewBag.ChartData);
}
}
conn.Dispose();
conn.Close();
}
}
return RedirectToAction("Index");
}
这里有一些Index.cshtml的代码
<script lang="javascript" type="text/javascript">
$(document).ready(function () {
var chartData = $.parseJSON("@ViewBag.ChartData");
var chartData = eval("@ViewBag.ChartData");
var line1 = eval("@ViewBag.ChartData");
var plot1 = $.jqplot('chart1', line1, {
title: 'Default Date Axis',
axes: { xaxis: { renderer: $.jqplot.DateAxisRenderer } },
series: [{ lineWidth: 4, markerOptions: { style: 'square' } }]
});
});
</script>
当我调试控制器时,我正在为var json
获取此示例输出:
[[[\"03.09.2012\",3.1790],[\"04.09.2012\",3.16],[\"05.09.2012\",3.17],[\"06.09.2012\",3.25],[\"07.09.2012\",3.2610],[\"10.09.2012\",3.2010],[\"11.09.2012\",3.1620],[\"12.09.2012\",3.22],[\"13.09.2012\",3.2190],[\"14.09.2012\",3.26],[\"17.09.2012\",3.2790],[\"18.09.2012\",3.2490],[\"19.09.2012\",3.2620],[\"20.09.2012\",3.2610],[\"21.09.2012\",3.2610],[\"24.09.2012\",3.2410],[\"25.09.2012\",3.25],[\"26.09.2012\",3.22],[\"27.09.2012\",3.1840],[\"28.09.2012\",3.2290],[\"01.10.2012\",3.25],[\"02.10.2012\",3.27],[\"03.10.2012\",3.2310],[\"04.10.2012\",3.2580],[\"05.10.2012\",3.32],[\"08.10.2012\",3.30],[\"09.10.2012\",3.28],[\"10.10.2012\",3.25],[\"11.10.2012\",3.2790],[\"12.10.2012\",3.2490],[\"15.10.2012\",3.24],[\"16.10.2012\",3.2750],[\"17.10.2012\",3.36],[\"18.10.2012\",3.3610],[\"19.10.2012\",3.3990],[\"22.10.2012\",3.37],[\"23.10.2012\",3.32],[\"24.10.2012\",3.3290],[\"25.10.2012\",3.3310],[\"26.10.2012\",3.3720],[\"29.10.2012\",3.3810],[\"30.10.2012\",3.3690],[\"31.10.2012\",3.4290],[\"01.11.2012\",3.45],[\"20.11.2012\",3.21],[\"21.11.2012\",3.29],[\"22.11.2012\",3.31],[\"23.11.2012\",3.35],[\"26.11.2012\",3.36],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]]
VS Debugger并未在行var line1 = eval("@ViewBag.ChartData");
中的Index.cshtml中停止。
在使用firebug调试Javascript时,我将line1
作为undefined
。
我做错了什么?我希望,ViewBag.ChartData = json;
会导致line1
填充json
的值。
答案 0 :(得分:2)
问题在于这一行:
return RedirectToAction("Index");
这样你就失去了Viewbag的内容..
如果您想使用ViewBag,您应该为DrawChart
创建一个视图并放置:
return View();