Razor/JavaScript and trailing semicolon Razor/JavaScript and trailing semicolon javascript javascript

Razor/JavaScript and trailing semicolon


Is this a bug in Razor?

Absolutely not. Run your application, and it will work as expected.

It is a bug in the tools you are using (Visual Studio 2012, ReSharper, ...) that are incapable of recognizing perfectly valid syntax and warning you about something that you shouldn't be warned about. You could try opening an issue on the Microsoft Connect site and signalling this bug if that hasn't already been done.


Since this still seems to be happening and it is a nuisance I figured I will at least let others know what I ended up using as a "hack". I don't want to ignore the warning and would rather accept a hokier syntax (and yes someone is going to say this will kill performance :))

What I use as a workaround is to use a client side addition at the end. For me this error occurred on defining an "integer" constant, so

window.foo = @(Model.Something);

gave me the good old semicolon error. I simply changed this to:

window.foo = @Model.Something + 0;

(In the stated questions case you should just be able to add '', so + ''.

I know there is a whole another addition happening on the client and it isn't elegant, but it does avoid the error. So use it or don't, but I prefer this over seeing the warning/error.

If someone knows of a server-side syntactical workaround for this I would prefer this to the client-side one, so please add.


I found that wrapping the Razor syntax in a JavaScript identity 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>