Reputation: 17233
I am trying to convert a model object to a json object. I tried doing this
<script type="text/javascript">
var myobj = '@Html.Raw(Json.Serialize(Model.MyObject))';
</script>
However the above myobj
is string. I tried converting it to an object using this
var myobj = JSON.parse(myobj)
However the above gives a parsing error.
My question is what is the fastest way to convert Model.MyObject
to a json object.
Upvotes: 0
Views: 2478
Reputation: 11
Try
<script type="text/javascript">
var myobj = @Html.Raw(Json.Encode(Model.MyObject));
</script>
Upvotes: 1
Reputation: 18973
I tried to reproduce simple model, it still worked.
Can you try to serialize all your model after that get MyObject property?
<script type="text/javascript">
var myobj = '@Html.Raw(Json.Serialize(Model))';
var json = JSON.parse(myobj);
var myObject = json.MyObject;
</script>
Upvotes: 0