不要在布局中渲染部分

Don't Render Section in the Layout

我使用 ASP.NET MVC 5 和空项目。

我有一个布局和一个视图。在布局中我有一个 RenderSection() 并且在渲染之前检查 .在视图中我添加了那个部分但不渲染部分。

我收到此错误:

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

我的布局是:

<body>
<div id="menu">
    @if(IsSectionDefined("Menu"))
    {
        RenderSection("Menu", required: false);
    }
    else
    {
        <span>this is the default</span>
    }
</div>
<div id="content">
    @RenderBody()
</div>

我的观点是:

 @{
    ViewBag.Title = "Index";
    Layout = "~/Views/_Layout.cshtml";
}

    <h2>Index</h2>
    @section Menu{
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
        </ul>
    }

为什么不起作用?

@ 符号也是 RenderSection

所必需的
@if(IsSectionDefined("Menu"))
{
  @RenderSection("Menu", required: false);
}
else
{
  <span>this is the default</span>
}