Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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

enter image description here

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?

like image 836
Samuel Avatar asked Apr 27 '26 14:04

Samuel


2 Answers

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.

like image 69
Joel Etherton Avatar answered Apr 29 '26 04:04

Joel Etherton


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;
like image 28
epascarello Avatar answered Apr 29 '26 04:04

epascarello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!