0

One of the properties of the model for my razor page is a string array i.e. string[].

This array contains data for a chart that I display on the page so I pass this array to a JavaScript function on the page for it to render the chart.

There are, however, cases when there's no data. In that situation, the assignment -- see below -- throws an error.

var data = @Model.chartData;

When this is rendered in my razor page, I'm seeing the following in JavaScript console:

var data = ;

So, I'm trying to handle that scenario but I couldn't figure out a way to assign an empty array to my data variable in my JavaScript function. I've tried the following and all are failing. Actually, IntelliSense is already telling me this is the wrong approach. How do I handle this scenario and assign an empty array to my variable?

Here's what I've tried:

function displayChart() {

   var data = @(Model.chartData.Length > 0 ? Model.chartData : new List<string>().ToArray());
   var data2 = @(Model.chartData .Length > 0 ? Model.chartData : []);
   var data3 = @(Model.chartData .Length > 0 ? Model.chartData : System.String[]);

}

These are all failing. What's the right way to assign an empty array to my JavaScript variable using Razor syntax?

P.S. Here's the screen shot of the error for the first and third options. The second option is giving me an error in IntelliSense. Here's the screen shot:

enter image description here

Sam
  • 26,817
  • 58
  • 206
  • 383
  • What error are you getting? Also what do you mean by `no data`, is `Model.chartData` `null`? – Guru Stron Sep 27 '20 at 22:11
  • See the screen shot I posted in original post – Sam Sep 27 '20 at 22:15
  • To create empty strings array in C# you can do `new string[0]`. In js just `[]` should work. – Guru Stron Sep 27 '20 at 22:16
  • Still getting the same error. Looks like `new string[0]` renders in Razor page as `System.String[]` and the error is `Uncaught SyntaxError: Unexpected Token ']'` – Sam Sep 27 '20 at 22:20
  • To answer your previous question, when I inspect my model, the `chartData` has `{string[0]}` as its value when there's no data. – Sam Sep 27 '20 at 22:22
  • And if `Model.chartData` - `var data = @Model.chartData;` works as expected? – Guru Stron Sep 27 '20 at 22:22
  • I started with `var data = @Model.chartData` but that was throwing a JS error showing `var data = ;` as if the value of `chartData` is null. This error would stop other JS code from executing. – Sam Sep 27 '20 at 22:24
  • 2
    Have you tried something like [this](https://stackoverflow.com/q/18470702/2501279) or [this](https://stackoverflow.com/q/23781034/2501279)? – Guru Stron Sep 27 '20 at 22:26
  • THANK YOU!!!!! This approach works! https://stackoverflow.com/questions/23781034/razor-mvc-populating-javascript-array-with-model-array – Sam Sep 27 '20 at 22:32
  • Glad it helped! – Guru Stron Sep 27 '20 at 22:33
  • I upvoted your comment but if you give this to me as an answer, I'll accept it. Again, thank you very much for your help! – Sam Sep 27 '20 at 22:33

0 Answers0