Search code examples
c#razornancy

NancyFx: @Each, @Current, and @EndEach fails Razor compilation but @Model.Property is fine


How can I fix this and make my .cshtml compile?

Summarize the problem I have an existing NancyFX 1.4.4 web application that uses Razor. It is working. I added a List of objects to my existing Model and referenced the List in my .cshtml file using @Model.Things, @Each.Model.Things, @Current.Property1, and @EndEach, but my .cshtml's Razor compilation fails with these errors:

  • [CS0103] Line: 380 Column: 24 - The name 'Each' does not exist in the current context (show)
  • [CS0103] Line: 381 Column: 83 - The name 'Current' does not exist in the current context (show)
  • [CS0103] Line: 382 Column: 24 - The name 'EndEach' does not exist in the current context (show)

Provide details and any research

  • I added this to my .cshtml, which does not error,

    alert("@Model.Things");

    and displays:

System.Collections.Generic.List`1[MyNamespace.Thing]

  • This is the .cshtml code that causes the compilation error:

    @Each.Model.Things <option value="@Current.Property1">blah</option> @EndEach

When appropriate, describe what you’ve tried

  • I downloaded the Super Simple View Engine from Nancy's github repo. It uses @Each.Users, @Current.Name, and @EndEach. It works fine.
  • I changed the List in my Model to IEnumerable and made no difference.
  • I put a breakpoint in my server-side Module to ensure there were objects in the List.
  • I checked my app.config and web.config for Razor references. Razor is working, so I didn't think that was the problem.

Solution

  • These are not Razor elements. They are defined in the Super-Simple-View-Engine itself. See the @Current definition in SuperSimpleViewEngine.cs and its usage in the SuperSimpleViewEngine demo.