2

Spark View Engine 可以像 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 Engine 中的等价物是什么?然后我在 Razor 中使用这样的代码,我想在 Spark 中做同样的事情。

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

1 回答 1

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>
于 2013-04-08T13:31:58.087 回答