As a test I'm converting a proof-of-concept app we've written from Web Forms to Razor, simply so we can evaluate it.
I've run into one problem so far that's making my head hurt..generating client-side Javascript...
<script type="text/javascript"> var jqGridIdList = "<%: Url.Action ("getidlist", "office", new { area = "reports" }) %>"; var availableIds = []; <% for (var i = 0; i < Model.Data.Count (); i++) { %> availableIds.push({ value : "<%: Model.Data.ElementAt (i).Text %>", label : "<%: Model.Data.ElementAt (i).Text %>" }); <% } %> </script>
<script type="text/javascript"> var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })"; var availableIds = []; @for(var i = 0; i < Model.Data.Count (); i++) { availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" }); } </script>
The compiler gives me the following error on the 'availableIds.push' line:
Compiler Error Message: CS1525: Invalid expression term '{'
It's obviously trying to compile it as C#...but how do I stop it?
Thanks,
Kieron
You can't. Razor is a . NET assembly and doesn't run on JavaScript or in a browser. It's meant to be executed server-side.
Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.
You need to wrap it in the pseudo element <text>
. This will switch the parser back to html mode and it will then parse the javascript as part of the html and not c#. The reason it happens is the @for()
is a c# block and anything treated within is also considered c# until it's escaped by an html tag. Since you probably don't want an html tag razor provides the <text>
tag to switch modes.
If you notice the difference in your asp.net webforms you end the <% for
line with a %>
which takes it out of c# mode. If you download the razor highlighter extension for visual studio 2010 it will help you see when code is treated as code and html is treated as html.
<script type="text/javascript"> var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })"; var availableIds = []; @for(var i = 0; i < Model.Data.Count (); i++) { <text>availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" });</text> } </script>
You can now use the @:
syntax for even more readability
<script type="text/javascript"> var jqGridIdList = "@Url.Action("getidlist", "office", new { area = "reports" })"; var availableIds = []; @for(var i = 0; i < Model.Data.Count (); i++) { @:availableIds.push({ value : "@Model.Data.ElementAt(i).Text", label : "@Model.Data.ElementAt(i).Text" }); } </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With