views:

101

answers:

4

I ran into an annoying problem: the following code gives a warning in Visual Studio.

<script type="text/javascript">
var x = <%: ViewData["param"] %>;
</script>

The warning is "Expected expression". Visual Studio gets confused, and all the javascript code after that is giving tons of warnings. Granted, it's all warnings, and it works perfectly fine in runtime - but it is very easy to miss real warnings among dozen of false positives.

It was working the same way in VS2008, and it wasn't fixed in VS2010. Does anybody know if there is a workaround, or a patch?

+1  A: 

You need to wrap the server side expression in quotes.

<script type="text/javascript">
var x = "<%: ViewData["param"] %>";
</script>
Dustin Laine
What if the value stored in `ViewData` is numeric and calculations are performed with the `x` variable?
Darin Dimitrov
parseInt("<%: ViewData["param"] %>");
Dustin Laine
Yes, it will work, but to do that clumsy stuff just to overcome VS bug seems like an overkill. After all, my goal is to write efficient clean code. Hopefully, it will be fixed in some service pack!
Felix
A: 

The approach you're taking is not a particularly sturdy one. Obviously it wouldn't work if you wanted to move your Javascript code to a separate file in order to improve your page load times....

You're better off using hidden form fields to move data from the server to client script. Alternatively, you can build the variable setter JS code programmatically by doing a bunch of string concatenation, then using the ClientScriptManager.RegisterClientScriptBlock() method to inject it into the output.

Warren
Thanks everybody for your comments. While I used the tag asp.net-*mvc*, I didn't explicitly mention that it is MVC code, and therefore code-behind and ClientScriptManager suggestions are only partially relevant.We can argue whether the approach is sturdy or not (obviously, it *will* work in a separate Javascript file just fine) - but I'd rather leave it for another time, since it is out of scope of this question.I am not sure why it is too much to ask of VS - it is quite intelligent to analyze aspx and server code independently in other places.
Felix
A: 

I believe that you ask from visual studio to understand too difficult thinks.

How visual studio can know what is inside the string that you pass ?, its parameters, its more code, what it is ???.... How VS can know what it is so can tread them that way ?

So they decide that everything on script tag there must be JavaScript.

My opinion is that if you won to avoid this error, Write render this JavaScript on code behind and not inside aspx page.

string cPlaceMeOnScript = "<script type=\"text/javascript\">var x =" + ViewData["param"] +";</script>"

and use any method to place this string on the start of your script.

Aristos
A: 

This is what I got from Microsoft:

Unfortunately, this is due to a design limitation of our editor. We have had multiple users provide the same feedback, but we do not have a good solution at this time, other than to avoid server-side generation of client script. We will consider this issue in our planning for the next Visual Studio release.

Felix