0

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 myobjis 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.

2 Answers 2

1

Try

<script type="text/javascript">
    var myobj = @Html.Raw(Json.Encode(Model.MyObject));
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more like to attract upvotes.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.