0

I'm building a Asp.Net application. I have this file

In TimeDomain.cshtml I want to display a chart, the data of this chart is on database. So this is the code of my controller:

public ActionResult TimeFrequency()
        {
            ArrayList arrayValue = new ArrayList();
            arrayValue.Add(0.5);
            arrayValue.Add(0.1);
            arrayValue.Add(0.5);
            arrayValue.Add(0.5);
            arrayValue.Add(0.3);
            arrayValue.Add(0.8);

            ViewBag.ArrayValue = arrayValue;
            return View();
        }

In the TimeDomain.cshtml I have this code to display the chart with random value:

var myConfig = {
                        type: 'bar',
                        backgroundColor: "#FFF",
                        plot: {
                            lineColor: "rgba(151,187,205,1)",
                            lineWidth: "2px",
                            backgroundColor2: "rgba(151,187,205,1)",
                            marker: {
                                backgroundColor: "rgba(151,187,205,1)",
                                borderColor: "white",
                                shadow: false
                            }
                        },
                        plotarea: {
                            backgroundColor: "white"
                        },
                        series: [{
                            values: getRandomValue(),
                            lineColor: "rgba(220,220,220,1)",
                            lineWidth: "2px",
                            alpha: 0.5,
                            borderWidth: "2px",
                            borderColor: "#C7C7C7",
                            borderTop: "2px solid #C7C7C7",
                            borderBottom: "0px",
                            backgroundColor1: "rgba(220,220,220,1)",
                            backgroundColor2: "rgba(220,220,220,1)",
                            marker: {
                                backgroundColor: "rgba(220,220,220,1)",
                            }

                        }]
                    }

In this line code:

 values:getRandomValue(),

the method getRandomValue return an array of values. I want to remove this method and read the value from controller

 ViewBag.ArrayValue = arrayValue;

How can I do this?

2 Answers 2

1

You can convert c# array to js array like that

var jsArray= @Html.Raw(Json.Serialize(ViewBag.ArrayValue));
Sign up to request clarification or add additional context in comments.

Comments

0

You have to take ViewBag.ArrayValue in javascript variable as below:

<script type="text/javascript">
     var dataArray= @Html.Raw(Json.Serialize(ViewBag.ArrayValue));
</script>

Change your code like below in myConfig of chart configuration

 values:dataArray

2 Comments

I have this error, 'JSon' does not contain a definition 'Serialize'
Try with @Html.Raw(ViewBag.ArrayValue). Remove Json.Serialize

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.