Skip to content

Instantly share code, notes, and snippets.

@IliaIdakiev
Created September 5, 2013 11:33
Show Gist options
  • Save IliaIdakiev/6448992 to your computer and use it in GitHub Desktop.
Save IliaIdakiev/6448992 to your computer and use it in GitHub Desktop.
Razor Parse C# List To Javascript array
var arr = JSON.parse('@Html.Raw(Json.Encode(@Model.DuplicateIds))')
@SiebeSijbers
Copy link

doesnt work

@OhmSharma2
Copy link

OhmSharma2 commented Sep 28, 2021

Replace Encode with Serialize and it will work.

https://stackoverflow.com/questions/50056518/ijsonhelper-does-not-contain-a-definition-for-encode

Here is an example that worked for me. I am calling a C# method from my Model that returns a list of USA locations and converting that list it to a JavaScript array.

var something = JSON.parse('@Html.Raw(Json.Serialize(Model.usaLocations()))');

@rakeshfrom1984
Copy link

this works for me. Thanks

@stolarinhoo
Copy link

var something = JSON.parse('@Html.Raw(Json.Serialize(Model.usaLocations()))');

Works fine, thank You!

@TamaraMart28
Copy link

doesn't work((

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment