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>
最佳答案
使用#
来指示一行仅是代码,因此:
# 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>
关于asp.net-mvc-4 - Spark View 引擎中的 C# 代码块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866902/