1

At one point, I had no problem collecting the raw model by calling:

@Html.Raw(Json.Encode(Model));

but now I'm getting this error:

Uncaught SyntaxError: Unexpected toke ILLEGAL

in Chrome and IE and it's point right at that '@' character

I've this (as suggested in another stackoverflow post):

var getRawLayoutData = function() { return @Html.Raw(Json.Encode(Model)); }
var rawModelData = getRawLayoutData();

and this (also suggested in another stackoverflow post):

var rawModelData = [@Html.Raw(Json.Encode(Model))][0];

and nothing seems to work.

All I want to do is pass back some modified model data from the server to the client so I can react to the changing model data.

Any suggestions??

9
  • @(Html.Raw(Json.Encode(Model))) Commented Nov 16, 2015 at 17:27
  • 2
    I haven't worked with razor extensively, but I generally do @{ } for block statements, @() seems a bit odd to me.. for clarification, this snippet is in a cshtml or vbhtml page that is being processed, right? Commented Nov 16, 2015 at 17:32
  • What file is this being done in? the cshtml? Commented Nov 16, 2015 at 17:33
  • @kformeck, please respond more timely.. I see two different errors going on here.. one is in your user-agent (browser) - which indicates that '@Html.Raw` isn't being processed... your browsers/user-agents should never see razor code.. they should only see the output; And the second error is in ASP.NET (your title is the only indication of this error).. this suggests your doing an @Html.Raw on a file/code that includes the code snippet you've provided us here... Commented Nov 16, 2015 at 17:52
  • 1
    .js file will not recognize @Html not in the past or present Commented Nov 16, 2015 at 19:02

3 Answers 3

2

You could always try moving your script out of the .js file and putting it into a partial view.

use dynamic as your model type and make your partial view code something like this.

@model dynamic
<script type="text/javascript">
    var getRawLayoutData = function() { return @Html.Raw(Json.Encode(Model)); }
    var rawModelData = getRawLayoutData();
</script>

Then, where you're loading your javascript file in your main view, just use this instead

@Html.Partial("_Scipt", Model)
Sign up to request clarification or add additional context in comments.

Comments

2

Razor code only works in cshtml or vbhtml files. JS files are not preprocessed by Razor. Therefore, you were just sending the Razor code directly to the browser as JS (invalid JS). Short of just dumping all the JS code to the view, you can merely set the variable in view and then access it in your external JS file. I suggest using a namespace, though, so you don't pollute the global namespace and risk collisions with other JavaScript code.

View

<script>
    var MyNamespace = MyNamespace || {};
    MyNamespace.RawModelData = @Html.Raw(Json.Encode(Model));
</script>
<script src="/path/to/external.js"></script>

Then, in your external JS, you can reference MyNamespace.RawModelData as needed.

1 Comment

Thanks sir! This worked for me with separate js file.
0

Try changing it to @(Html.Raw(Json.Encode(Model))); instead. That should fix your problem. Looking back at my projects, this is the syntax I use and it doesn't give me any problems.

5 Comments

hey @jgabb, I think it's too early to tell whether this was the issue at all.. still, your suggestion would work under normal circumstances..
this didn't work, is this because I'm in a .js file?
Yes. That is the reason.
jgabb: Thanks for clarifying; should I just embed a simple script into my cshtml file to resolve this issue?
Yes, just include your script in your cshtml file. Remember not to use @(Html.Raw(Json.Encode(Model))); in your js file though.. Do that in your cshtml file.

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.