if 语句中的 RenderBody() 方法?
RenderBody() method in an if statement?
我有一个项目,其中有很多具有相同布局的视图,所以我使用 _ViewStart 使它们都具有相同的布局。在该布局中,我使用@RenderBody() 将页面内容填充到布局中。行得通。
现在我正在尝试将 @RenderBody() 放入 if 语句中。在 else 语句中我放了一些 html。它看起来像这样:
...*/
@if (condition == true) {
@RenderBody()
} else {
<p>Some text if the condition is false</p>
}
/*...
当我 运行 此代码且条件为假时,我收到此错误:
System.Web.HttpException: The "RenderBody" method has not been called for layout page "~/Views/Shared/_BasicLayout.cshtml".
所以我的问题是:
如何将@RenderBody() 放在 if 语句中?
根据要求,这里是整个 _BasicLayout body:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li @(@ViewBag.Title == "Films" ? "class=active" : "")>@Html.ActionLink("Films", "List", "FilmSelectie")</li>
<li @(@ViewBag.Title == "Index" ? "class=active" : "")>@Html.ActionLink("Voorstellingen", "Index", "Voorstelling")</li>
<li @(@ViewBag.Title == "Nieuwsbrief" ? "class=active" : "")>@Html.ActionLink("Nieuwsbrief", "Nieuwsbrief", "Nieuwsbrief")</li>
<li @(@ViewBag.Title == "Contact" ? "class=active" : "")>@Html.ActionLink("Contact", "ContactBreda", "Contact")</li>
<li @(@ViewBag.Title == "Login" ? "class=active" : "")>@Html.ActionLink("Login", "Login", "User")</li>
</ul>
</div>
</div>
</nav>
<div class="row panel" style="display:block; box-shadow: none;margin-top:5px;margin:0;margin-left:0px;">
<div id="film informatie" class="col-xs-3">
</div>
<div class="col-xs-8" style="display:block; box-shadow:none; margin-top:20px;">
@if (0 == 1) {
@RenderBody()
} else {
<h2>Geen toegang!</h2>
<p>U heeft geen toegang tot deze pagina. Log in met een geschikt account om op deze pagina te komen.</p>
}
</div>
</div>
<footer>
<div class="panel-footer" style="display:block; text-align:center;">
<p>
© 2015 Cinema Jam || Chasséveld 1, Breda ||
<a href="https://www.facebook.com/CinemaJamBreda" target="_blank"><img style="width:20px; height:20px;" src="~/Content/facebook.png" alt="CinemaJam FacebookPagina"></a>
<a href="https://twitter.com/CinemaJamB3" target="_blank"><img style="width:20px; height:20px;" src="~/Content/twitter.png" alt="CinemaJam TwitterPagina"></a>
</p>
</div>
</footer>
(我无法在堆栈溢出中粘贴 header 和 body 的开头,它包含对 bootstrap 的引用)
这个问题的解决方法是使用[授权]。我现在正在学习本教程:
http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF
感谢 DionV。
我有一个项目,其中有很多具有相同布局的视图,所以我使用 _ViewStart 使它们都具有相同的布局。在该布局中,我使用@RenderBody() 将页面内容填充到布局中。行得通。
现在我正在尝试将 @RenderBody() 放入 if 语句中。在 else 语句中我放了一些 html。它看起来像这样:
...*/
@if (condition == true) {
@RenderBody()
} else {
<p>Some text if the condition is false</p>
}
/*...
当我 运行 此代码且条件为假时,我收到此错误:
System.Web.HttpException: The "RenderBody" method has not been called for layout page "~/Views/Shared/_BasicLayout.cshtml".
所以我的问题是:
如何将@RenderBody() 放在 if 语句中?
根据要求,这里是整个 _BasicLayout body:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li @(@ViewBag.Title == "Films" ? "class=active" : "")>@Html.ActionLink("Films", "List", "FilmSelectie")</li>
<li @(@ViewBag.Title == "Index" ? "class=active" : "")>@Html.ActionLink("Voorstellingen", "Index", "Voorstelling")</li>
<li @(@ViewBag.Title == "Nieuwsbrief" ? "class=active" : "")>@Html.ActionLink("Nieuwsbrief", "Nieuwsbrief", "Nieuwsbrief")</li>
<li @(@ViewBag.Title == "Contact" ? "class=active" : "")>@Html.ActionLink("Contact", "ContactBreda", "Contact")</li>
<li @(@ViewBag.Title == "Login" ? "class=active" : "")>@Html.ActionLink("Login", "Login", "User")</li>
</ul>
</div>
</div>
</nav>
<div class="row panel" style="display:block; box-shadow: none;margin-top:5px;margin:0;margin-left:0px;">
<div id="film informatie" class="col-xs-3">
</div>
<div class="col-xs-8" style="display:block; box-shadow:none; margin-top:20px;">
@if (0 == 1) {
@RenderBody()
} else {
<h2>Geen toegang!</h2>
<p>U heeft geen toegang tot deze pagina. Log in met een geschikt account om op deze pagina te komen.</p>
}
</div>
</div>
<footer>
<div class="panel-footer" style="display:block; text-align:center;">
<p>
© 2015 Cinema Jam || Chasséveld 1, Breda ||
<a href="https://www.facebook.com/CinemaJamBreda" target="_blank"><img style="width:20px; height:20px;" src="~/Content/facebook.png" alt="CinemaJam FacebookPagina"></a>
<a href="https://twitter.com/CinemaJamB3" target="_blank"><img style="width:20px; height:20px;" src="~/Content/twitter.png" alt="CinemaJam TwitterPagina"></a>
</p>
</div>
</footer>
(我无法在堆栈溢出中粘贴 header 和 body 的开头,它包含对 bootstrap 的引用)
这个问题的解决方法是使用[授权]。我现在正在学习本教程: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF 感谢 DionV。