Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert view model data to json

I'm trying to convert my view model data to JSON, however, I can't seem to access @Json.Encode. I've added the System.Web.Helpers reference, but that didn't work. What am I doing wrong?

like image 896
CinnamonBun Avatar asked Jan 11 '23 10:01

CinnamonBun


1 Answers

Put this at the top of your view

@using Newtonsoft.Json

And then you can do this

@Html.Raw(@JsonConvert.SerializeObject(ViewBag.MyStuff))
like image 125
Greg Ennis Avatar answered Jan 25 '23 09:01

Greg Ennis