0

I am attempting to extract data from a Json object in .NET MVC 4.

Here is a code snippet of my razor view:

var data = (function($)
           {
            var dataModel = @(new HtmlString(new 
             System.Web.Script.Serialization.JavaScriptSerializer()
              .Serialize(Model)));

This won't run due the "@" sign within the javascript code.

How can I fix this so that razor switches back to .NET managed mode?

Thanks.

1 Answer 1

1

Like this:

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

2 Comments

That's not an error, it's a warning that you could completely and safely ignore. Run the code and it will work perfectly fine.
Thanks Darin.. it does run fine, but when I try to publish, it fails due to the warning. Is there a way I can disable that?

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.