【问题标题】:For loop in Spark view engine / ASP.NET MVCSpark 视图引擎/ASP.NET MVC 中的 For 循环
【发布时间】:2012-12-30 10:31:59
【问题描述】:

我将 Spark 与 WPF 一起使用。这很好用,我可以格式化模板并从自定义 DTO(我的模型)中提取标量属性。我很难让 for 循环工作。通过查看the Spark documentation,我应该能够做到这一点,但我得到了一个火花异常:

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

以下代码在模板中可以正常工作以从模型中提取 Name 属性:<h1>${Name}</h1> 但 for 循环会生成异常:

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

【问题讨论】:

    标签: c# templates templating spark-view-engine


    【解决方案1】:

    我需要导入集合命名空间以及包含 IList 中对象类型的命名空间:

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

    【讨论】:

    • 您的基本框架命名空间System.* 和您的常用用户命名空间应在_global.spark 中声明,即Web.config 中的spark 配置部分,或在构造@987654324 时添加到SparkSettings @所以你不必在每个视图文件中重复它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2011-03-11
    • 2011-01-09
    • 2010-12-20
    • 2010-10-13
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多