0

I am trying to pass a Session value from MVC controller to my jascript variable. Here is what i have tried

--ASP.NET MVC Controller--

Session["UserGroups"] = model.Groupes as List<string>;

--JS----

<script>
    var my_groups = '@Session["UserGroups"]'; 
    console.log(my_groups);
</script>

--Output--

System.Collections.Generic.List`1[System.String]

I would appreciate any help or another approach i can use to achieve my goal. Thanks

2
  • You need to convert your session object to json. Some answers here should help: stackoverflow.com/questions/18470702/… Commented Dec 15, 2020 at 16:29
  • It's a List, which doesn't directly translate to a JS object. It's is implicitly calling ToString so that's why you see that. Did you want it as a comma-separate string, an array, or some other type, perhaps JSON serialized? Commented Dec 15, 2020 at 16:29

0

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.