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

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
Kenzo
  • 1,767
  • 8
  • 30
  • 53
  • You need to convert your session object to json. Some answers here should help: https://stackoverflow.com/questions/18470702/how-do-i-convert-a-c-sharp-liststring-to-a-javascript-array – David Spence Dec 15 '20 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? – Nikki9696 Dec 15 '20 at 16:29

0 Answers0