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))')
@chenHungTzu
Copy link

you are lier ...........

@arimaze
Copy link

arimaze commented Sep 8, 2017

yes that works

@vijaytbirari
Copy link

Yes working

@oznc
Copy link

oznc commented Mar 19, 2018

yes!

@quasi635
Copy link

Thank you!!! Worked perfect

@frlmoney
Copy link

frlmoney commented Jul 9, 2019

yes that works

@ezuro
Copy link

ezuro commented Sep 19, 2020

Finally! Thanks!

@alirezakylix
Copy link

I LOVE YOUUUUUUUUUUUUUUUUUU.

@Chuong2102
Copy link

thanks you so much !!! it's work !!!

@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