C# - 在三元运算符中呈现部分
C# - Rendering a partial in a ternary operator
我有条件地使用三元运算符渲染页脚。我在做 @RenderPage
,即使它有效,也意味着有一个控制器以及一些其他额外代码。
我 运行 遇到了 ; expected
错误,根据 Whosebug 和一些文档,这是一个一般错误,可能由多种不同的原因引起。
我的语法正确吗?还是我遗漏了什么?
// 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")));
使用您更容易阅读和维护的那些。
我有条件地使用三元运算符渲染页脚。我在做 @RenderPage
,即使它有效,也意味着有一个控制器以及一些其他额外代码。
我 运行 遇到了 ; expected
错误,根据 Whosebug 和一些文档,这是一个一般错误,可能由多种不同的原因引起。
我的语法正确吗?还是我遗漏了什么?
// 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")));
使用您更容易阅读和维护的那些。