Is it possible or is there a workaround to use Razor syntax within JavaScript that is in a view (cshtml
)?
I am trying to add markers to a Google map... For example, I tried this, but I'm getting a ton of compilation errors:
<script type="text/javascript">
// Some JavaScript code here to display map, etc.
// Now add markers
@foreach (var item in Model) {
var markerlatLng = new google.maps.LatLng(@(Model.Latitude), @(Model.Longitude));
var title = '@(Model.Title)';
var description = '@(Model.Description)';
var contentString = '<h3>' + title + '</h3>' + '<p>' + description + '</p>'
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: latLng,
title: title,
map: map,
draggable: false
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}
</script>
One thing to add - I found that Razor syntax hilighter (and probably the compiler) interpret the position of the opening bracket differently:
I prefer "<!--" "-->" like a "text>"
There is also one more option than @: and
<text></text>
.Using
<script>
block itself.When you need to do large chunks of JavaScript depending on Razor code, you can do it like this:
Pros of this manner is that it doesn't mix JavaScript and Razor too much, because mixing them a lot will cause readability issues eventually. Also large text blocks are not very readable either.
Use the
<text>
pseudo-element, as described here, to force the Razor compiler back into content mode:Update:
Scott Guthrie recently posted about
@:
syntax in Razor, which is slightly less clunky than the<text>
tag if you just have one or two lines of JavaScript code to add. The following approach would probably be preferable, because it reduces the size of the generated HTML. (You could even move the addMarker function to a static, cached JavaScript file to further reduce the size):Updated the above code to make the call to
addMarker
more correct.To clarify, the
@:
forces Razor back into text mode, even thoughaddMarker
call looks a lot like C# code. Razor then picks up the@item.Property
syntax to say that it should directly output the contents of those properties.Update 2
It's worth noting that View code really isn't a good place to put JavaScript code. JavaScript code should be placed in a static
.js
file, and then it should get the data that it needs either from an Ajax call or by scanningdata-
attributes from the HTML. Besides making it possible to cache your JavaScript code, this also avoids issues with encoding, since Razor is designed to encode for HTML, but not JavaScript.View Code
JavaScript code
None of the previous solutions work correctly... I have tried all the ways, but it did not give me the expected result... At last I found that there are some errors in the code... And the full code is given below.
You're trying to jam a square peg in a round hole.
Razor was intended as an HTML-generating template language. You may very well get it to generate JavaScript code, but it wasn't designed for that.
For instance: What if
Model.Title
contains an apostrophe? That would break your JavaScript code, and Razor won't escape it correctly by default.It would probably be more appropriate to use a String generator in a helper function. There will likely be fewer unintended consequences of that approach.