0

how can I convert a list of integer arrays from C# to JavaScript? Is there a way to parse it? I've tried something along these lines:

@List<int[]> linkedRazorResources = Model.LinkedEvents;
for(var i = 0; i<@linkedRazorResources.Count;i++){
  linkedResources[i] = @linkedRazorResources[i];
}

I can't seem to access the index in @linkedRazorResources[i] though.

3 Answers 3

2

You can use Html.Raw and Json.Encode methods in your .cshtml:

 <script type="text/javascript">
        var linkedRazorResourcesForJs = @Html.Raw(Json.Encode(linkedRazorResources));
 </script>

The you can use this variable in your .js files.

Json.Encode converts a data object to a string that is in the JSON. And after that Html.Raw returns markup that is not HTML encoded.

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

Comments

1

Try a JSON Encode:

@Html.Raw(Json.Encode(linkedRazorResources))

This will output the raw JavaScript Object, and you can use this directly inside your JavaScript <script> tags.

Comments

1

You can do like below,

 <script type="text/jscript">
   var jsonVariable =      @(Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(linkedRazorResources)));//using Newtonsoft
  //or you can use below
  //var jsonVariable = @Html.Raw(Json.Encode(linkedRazorResources))
  </script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.