Which is the right syntax?

Apr 29, 2011 at 10:50 PM

Hi,
I have the following code in my template, with razorengine it process successfully:

@if (Model.List.Count > 0){
	<ol>
		@foreach(var item in Model.List){	
			<li>@item.Name</li>
		}
	</ol>
}

The if statement is required because the Model has two more lists and the template renders different h1 depending of what list is present.
but testing this code inside an asp.net mvc web application with Razor views it throws the following exception when compiling the view: 

Unexpected  "foreach" keyword after "@" character.  Once inside code, you do not  need to prefix constructs like "foreach" with "@".

Is RazorEngine using a different syntax than Razor View Engine?

Coordinator
May 2, 2011 at 3:13 PM

Technically the razor engine here is the base razor engine that is used by mvc, however mvc has added more features (@model, @helper, and other such directives).

However, what you've posted is compatible with the base razor parser so it should work just fine in both engines. A quick test shows that the above code works. Perhaps the foreach also exists somewhere else? SubSection maybe?

May 2, 2011 at 3:34 PM

Yes, foreach appears more than once in my templates, the model has three lists.

Coordinator
May 2, 2011 at 4:06 PM

Could you post the code for each foreach?