0

I got a Dictionary<string, List<string>> in my controller , and I have a js file in /wwwroot/view-resources, so How to pass the Dictionary to js file

I have try var result = '@Model.Dic' ,and I got a string-type variable @Model.Dic,

I also have try to use a hidden input with value="@{@Model.EditList}", and I got a result

System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[System.String]]

I use abp with .netcore

1 Answer 1

1
  1. Serialize the dictionary into a JSON string in C#.
  2. Parse the JSON string in JavaScript.
// View.cshtml

using Newtonsoft.Json;

<script>
var jsonString = '@JsonConvert.SerializeObject(Model.Dic)';
var dictionary = JSON.parse(jsonString);

console.log(dictionary);
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry to reply late,I have try the code, but I got a string "@JsonConvert.SerializeObject(Model.Dic)", it seems that My description is not accurate enough, my javascript code is not in view.cshtml, It is a external file
by the way, I have got the data with an action in controller and an ajax function in js file, but if there is any way to pass ViewModel data to external javascript file, I really want to konw it
You can't do that and probably shouldn't. JS files are likely cached. What are you trying to achieve? You probably have an XY problem.

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.