1

I would like to access a List<Records> where Records is a struct, from a JS script. After a bit of googling (especially question 29098558 here) i Json serialized my object and retrieve it in the script.

I should mention it's my first time at JS so syntax may be totally wrong.

In C#

  string serializedResults = JsonConvert.SerializeObject(RecordsList);

(in my context the records are actually in a Dictionnary<string, Records> but i hide the access through dictionnary because i supposed it isn't relevant here)

In Javascript

<script type="text/javascript">
    function setupBarChart() {
        var x = [];
        var y = [];
        var source = "<%= serializedRecords %>";

        // I would like to put Records fields in y and x tab to use it as data for the axes of an histogram.
        for (var i = 0; i < source.length; i++) {

        }  
    }
</script>

My questions are :

  • what is the type of source ? Is it just a string ? Does it create an equivalent of List in JS ?
  • How do I access my Records fields from JS ?
  • Does source.length gives me the number of records in the list ??
2
  • Where are you calling JsonConvert.SerializeObject? You have declared serializedResults but you are trying to use serializedRecords. Looks like some typo. Commented Apr 6, 2017 at 11:23
  • Some typo indeed, it's serializedRecords. I'm calling SerializeObject from the ascx.cs associated to the ascx file where I write my JS. Commented Apr 6, 2017 at 11:27

1 Answer 1

1

what is the type of source ? Is it just a string ? Does it create an equivalent of List in JS ?

In the code shown, it is a string, but I think the code shown is very wrong - the quotes are incorrect and should be removed, to:

var source = <%= serializedRecords %>;

(note, I'm not going to opine on encoding in the above, but: be careful and check it is safe)

leaving (in the rendered html):

var source = [{"foo":123,"bar":"def"},{...},{...}];

now the type is a javascript array.

How do I access my Records fields from JS ?

via source, which is an array

Does source.length gives me the number of records in the list ??

yes, if you lose the quotes

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry for the wrong syntax, first time at javascript. I understand why the quotes make source a string. However VisualStudio warn me of a syntax error without the quotes, i must have forgotten something else elsewhere.
@YthioCsi nah, I think Visual Studio is just struggling to parse your intent - does it work? I suspect it will
I wish i could deploy solution but i can't right now... ... ... Anyway, i read the article about JS arrays on W3School website and I wondered if source is an associative array, if so, it tells "all array methods and properties will produce incorrect results", so source.length will return 0 no matter what, doesn't it ? (i can use a foreach instead so it's more a question than a problem)
@YthioCsi it is just a javascript array - length will work just fine; and no, you can't use a foreach - javascript doesn't have a foreach keyword; there are methods like forEach or $.each, of course
Would my view update each time the serializedRecords value changed ? Like a INotifyPropertyChange implementation in WPF ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.