2013-04-07 33 views
2

Spark View Engine可以處理C#代碼塊如Razor嗎?例如,我在剃刀中有以下內容:Spark View Engine中的C#代碼塊?

@{ 
    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

使用#指示行代碼而已,所以:

# 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> 
+0

I would al所以主張將這些變量放在基本控制器或全局過濾器中的ViewData中,以便它們可用於任何視圖。 – 2013-04-08 13:34:25

+0

我遵循你的第一個建議,但我得到的錯誤:名稱'currentController'在當前上下文中不存在。 :(Q也更新了。 – Ciwan 2013-04-09 10:22:24

+0

嗯..它對我來說工作得很好。在獲得的錯誤信息中,視圖的完整源代碼應該在消息中。檢查源代碼並查看是否可以找到變量的存在位置使用和聲明的位置 – 2013-04-09 14:47:06