weixin_33739523 2014-04-25 08:53 采纳率: 0%
浏览 31

MVC中的条件部分

I have a twin Layout setup that renders either the entire page (with navigation) or just the contents depending on whether it was an Ajax request or not. I achieve this like so:

_LayoutRightPanel.cshtml

@{
    Layout = !Request.IsAjaxRequest() ? "~/Views/Shared/_Layout.cshtml" : null;
}

This works fine for most pages, but pages that are requested via Ajax that contains @section{}s dont work (they do when loaded normally). I get the following error:

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutRightPanel.cshtml": "scripts".

At the bottom of my _LayoutRightPanel I do the following to render scripts, either forcing the section to render on the main layout or rendering it on the current page if its an ajax request

@if (Request.IsAjaxRequest())
{
    RenderSection("scripts", false);
}
else{
    @section scripts{
        @RenderSection("scripts", false)
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33744854 2014-04-25 09:00
    关注
    @section scripts{
        @RenderSection("scripts", false)
    }
    

    Why are you rendering the scripts section inside of the scripts section? I don't see how that can end in any way but errors or infinite recursion (which ends in an error).

    Edit This is incorrect. See my update below.

    RenderSection() should be used on the _Layout page (or any page that is wrapped around the current view). This is the target location (where you want the section to end up)

    @section scripts{} should be used on your (Partial)View. This is the source location (where you define the section's contents).

    Having both references to the same section on the same page defeats the purpose of why you should be using sections in the first place (to pass certain HTML parts to other, wrapping views (e.g. _Layout.cshtml))

    Slight update
    To directly answer your question, I think the error is thrown because the RenderSection call that's inside of the @section cannot find any section called "scripts" because it has not yet been defined (you're in the process of defining it).

    Update

    According to this SO question, duplicate names should not be an issue (so you can disregard my comment below).

    However, the root cause of the issue (in that question) was that the script sections weren't chained (i.e. you can't render the section 2 layout pages up, it has to be in the specific Layout page of your view (and then keep chaining it upwards to the next Layout page).

    Can you confirm that to be set up as intended?

    Maybe offtopic, but is there any added value to passing the scripts section to the upper Layout page? Since you're going to render the section either way, should it not work if you always place it in the _LayoutRightPanel page?

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘