Search code examples
c#templatestemplatingspark-view-engine

For loop in Spark view engine / ASP.NET MVC


I'm using Spark with WPF. This works fine and I'm able to format a template and pull in scalar properties from a custom DTO (my model). I'm having difficulty getting for loops to work though. From looking at the Spark documentation, I should be able to do this but I get a spark exception:

Dynamic view compilation failed.

c:\Users\Echilon\AppData\Local\Temp\tmp4490.tmp(73,73): error CS0103: The name 'Sections' does not exist in the current context

The following code works fine in the template to pull the Name property from the model: <h1>${Name}</h1> but the for loop generates the exception:

<for each="var sect in Sections">
    ${#sect.Name}
    <!-- HTML for each element omitted -->
</for>

Solution

  • I needed to import the collections namespace as well as the namespace containing the type of objects in my IList:

    <use namespace="System.Collections.Generic"/>
    <use namespace="System.Web.Mvc"/>
    <use namespace="MyOtherAssembly"/>
    <viewdata Sections="IList[[ISection]]"/>