我有一个包含大量具有相同布局的视图的项目,因此我使用_ViewStart使它们都具有相同的布局。在该布局中,我使用@RenderBody()来填充页面内容的布局。这很有效。
现在我试图将@RenderBody()放在if语句中。在else语句中我放了一些html。它看起来像这样:
...*/
@if (condition == true) {
@RenderBody()
} else {
<p>Some text if the condition is false</p>
}
/*...
当我运行此代码并且条件为false时,我收到此错误:
System.Web.HttpException:尚未为布局页面“〜/ Views / Shared / _BasicLayout.cshtml”调用“RenderBody”方法。
所以我的问题是:
如何将@RenderBody()放在if语句中?
根据要求,这是整个_BasicLayout主体:
<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>
(不知怎的,我无法在堆栈溢出中粘贴标题和正文的开头,它包含对bootstrap的引用)
答案 0 :(得分:0)
此问题的解决方案是使用[授权]。我现在正在关注这个教程: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF 感谢DionV。