简体   繁体   中英

Javascript syntax error in razor page

I found in this link a solution to transfer some data from my model to a javascript object. The problem is that I have an error in visual studio indicating that the semi-colon is invalid but when I try to run my application, it works perfectly.

Here is a screenshot that show the error:

在此输入图像描述

But if I remove the semi-colon, I now have this error:

在此输入图像描述

In anyways, a javascript statement should always have a semi-colon at the end of the line.

What means this error if when I run my application, all works fine?

You might try enclosing it in a block:

var serializedData = @{ 
    Html.Raw(NewtonSoft.Json.JsonConvert.SerializeObject(Model));
};

Edit: This answer only applies to VS 2010. An upgrade in VS 2012 breaks this workaround as well.

I am just starting with Razor/MVC4, but can you so something like this?

@{
    var serializedData = Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));
}
<script>
    ...
    var serializedData = @serializedData;

Wrap your razor code in quotes like so:

var myVar = '@serializedData';

As JavaScript duck types you should be ok in most use cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM