1

I'm trying to use jqplot with mvc4. 'im trying to get data for plot in the controller like the following:

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");
        }

Here some code of 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>

When I debug controller I'm getting this sample output for 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 doesnt stop in Index.cshtml in line var line1 = eval("@ViewBag.ChartData");. While debugging Javascript with firebug I'm getting line1 as undefined.

What am I doing wrong? I'd expect, that ViewBag.ChartData = json; would cause, that line1 will be filled with value from json.

4
  • 2
    As an aside note: The model consists of application data and business rules, and the controller mediates input, converting it to commands for the model or view In your code there is not separation of concerns. Your controller has SQL Code... Commented Nov 27, 2012 at 19:11
  • I'm learnig it and I don't know how can I abstract oracle data in model. Commented Nov 28, 2012 at 10:23
  • 2
    This is a very good tutorial to learn MVC mvcmusicstore.codeplex.com Commented Nov 28, 2012 at 11:10
  • I' reading a tutorial for mvc4. I'm reading about models. There is written: " You can then have the database created on the fly from your classes, which enables a very clean and rapid development workflow. " . Can I also abstract data from tables, that I already have? I don't need to abstract all columns(i.e. i need only two of 50). I need only read access and wanna use either web service or oracle. You know any sample code, so I can see how can abstract data for web-service or oracle as data source? Commented Nov 28, 2012 at 14:50

1 Answer 1

2

The problem is this line:

return RedirectToAction("Index");

With that you lose the content of Viewbag..

If you want to use ViewBag, you should create a view for DrawChart and put:

return View();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.