Razor / JavaScript and the end semicolon

Using Visual Studio 2012, on the Razor view page, in the JavaScript section, I get what, in my opinion, is a battle between the Razor syntax and the JavaScript syntax. In particular, the semicolon in the script section is marked intellisense and a compiler warning is sent (not an error):

'Warning 13 Syntax error.

If I delete it, then I get a recommendation to stop discharge ( ReSharper in this case, but just good practice).

<script type="text/javascript"> $().ready(function(){ var customer = @Html.Raw(ViewBag.CustomerJSON); // <- Razor (I think) doesn't like this semicolon }); </script> 

Is this a bug in Razor? If so, can I rewrite this to avoid this problem?

+58
javascript asp.net-mvc razor
Aug 24 '12 at 14:50
source share
7 answers

Is this a bug in Razor?

Absolutely not. Launch the application and it will work as expected.

This is a mistake in the tools that you use (Visual Studio 2012, ReSharper, ...) that are unable to recognize the completely correct syntax and warn you about what you should not warn about. You can try to open the problem on the Microsoft Connect website and report this error if it is not already done.

+65
Aug 25 2018-12-12T00:
source share

Since this is still happening, and this is a nuisance, I decided that at least I would let others know what I ultimately used as a β€œhack”. I don't want to ignore the warning and would rather agree with the hokier syntax (and yes, someone will say that this will kill performance :))

As a workaround, I use the addition of the client side at the end. For me, this error occurred while defining the constant "integer", therefore

 window.foo = @(Model.Something); 

gave me a good old semicolon. I just changed this to:

 window.foo = @Model.Something + 0; 

(In the stated question, you should simply add ``, therefore + ''.

I know that on the client there is another addition, and it is not elegant, but it helps to avoid an error. So use it or not, but I prefer it to see a warning / error.

If anyone knows of a server side syntax workaround for this, I would prefer it on the client side, so please add.

+24
Apr 01 '13 at
source share

I found that wrapping the Razor syntax in the JavaScript authentication function also makes the IDE happy.

 <script type="text/javascript"> @* I stands for Identity *@ function I(obj) { return obj; } $().ready(function(){ var customer = I(@Html.Raw(ViewBag.CustomerJSON)); }); </script> 
+7
Nov 15 '14 at 1:23
source share

This worked for me:

 var customer = @Html.Raw(ViewBag.CustomerJSON + ";") 
+3
Apr 02 '14 at 14:45
source share

Here's a workaround for booleans:

 var myBool = @(Model.MyBool ? "true;" : "false;") 
+1
Aug 07 '14 at 16:44
source share

It worked for me

 @Html.Raw(string.Format("var customer = {0};", ViewBag.CustomerJSON)); 
+1
Aug 08 '14 at 0:28
source share
 <script type="text/javascript"> $().ready(function(){ var customerName = ('@ViewBag.CustomerName'); // <- wrap in parens }); </script> 

Isn't it as simple as wrapping in parentheses? Putting values ​​through the console seems to work fine, with no side effects.

It works for strings, but it still gives an error for not quotes, but I still like this for string values. For numbers, you can simply use parseInt('@Model.TotalResultCount', 10) .

0
May 08 '15 at 12:36
source share



All Articles