Spark View引擎中的C#代码块?

时间:2013-04-07 19:38:21

标签: asp.net-mvc-4 spark-view-engine

Spark View引擎可以像Razor一样处理C#代码块吗?例如,我在Razor中有以下内容:

@{
   var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
   var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
   var currentPage = (currentController + "-" + currentAction ).ToLower();
}

Spark View引擎中的等价物是什么?然后我在Razor中使用这样的代码,我想在Spark中使用它。

<li @(currentPage == "home-index" ? "class = current" : "")>
    @Html.ActionLink("Home", "Index", "Home")
</li>

1 个答案:

答案 0 :(得分:4)

使用#表示一行只是代码,所以:

# var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
# var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
# var currentPage = (currentController + "-" + currentAction ).ToLower();

另外,如果你所做的只是声明变量,你可以改为:

<var currentController="ViewContext.RouteData.Values['controller'] as string ?? 'Home'" />

您的用法如下:

<li class="current?{currentPage == "home-index"}">
    ${Html.ActionLink("Home", "Index", "Home")}
</li>