我有条件地使用三元运算符渲染页脚。我正在做@RenderPage
,即使它有效,也意味着要有一个控制器以及其他一些额外的代码。
我遇到; expected
错误,根据StackOverflow和一些文档,这是一个一般性错误,可能是由许多不同的原因引起的。
我的语法正确吗?还是我想念的东西?
// in this section is a switch statement that sets isNewFooter to true or false depending on which page has loaded.
<div class="body-content">
@RenderBody()
@{
(isNewFooter ? Html.RenderPartial("~/Views/Shared/NewFooter.cshtml") : Html.RenderPartial("~/Views/Shared/OldFooter.cshtml"))
}
</div>
三元运算符用于评估不同的表达式,而不是执行不同的语句。您可以只使用标准的if
:
if (isNewFooter)
Html.RenderPartial("~/Views/Shared/NewFooter.cshtml");
else
Html.RenderPartial("~/Views/Shared/OldFooter.cshtml");
或重构常见的东西,以便您可以使用三元运算符:
Html.RenderPartial(String.Format("~/Views/Shared/{0}Footer.cshtml", (isNewFooter ? "New" : "Old")));
使用其中任何一个都更易于阅读和维护。