【问题标题】:RenderSection defined but not renderedRenderSection 已定义但未渲染
【发布时间】:2011-07-13 04:28:40
【问题描述】:

我正在尝试让我的网站的导航栏有点动态。 如果 categoryId 等于 0,我会删除一些按钮,否则我会全部显示:

_Layout.cshtml:

<body>
    <div class="page">
        <div id="header">
            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>     
            <div id="menucontainer">
                @if (IsSectionDefined("Navigation"))
                {
                    {RenderSection("Navigation", false);}
                }
                else
                {
                    <p>No navigation setup!</p>
                }
            </div>
        </div>
        <div id="main">
            <div id="contentwrapper">
                @RenderBody()
            </div>
            <div id="footer">
            </div>
        </div>
    </div>
</body> 

Index.cshtml:来自我的 HomeController

@model Project2.ViewModels.ProjectCategoryListViewModel

@{
    ViewBag.Title = "Home Page";
}

@section Navigation {
    @{Html.RenderAction("LayoutNav", "Home", new { CategoryId = 0 });}
}

<!-- Rest of the page's code -->

Index.cshtml:来自我的 CategoryController

@model Project2.ViewModels.Categories.CategoryIndexViewModel

@{
    ViewBag.Title = "Category Index";
}

@section Navigation {
    @{Html.RenderAction("LayoutNav", "Home", new { CategoryId = Model.Category.Id });} 
}

<!-- Rest of the page's code -->

我尝试了完全相同的设置,但没有 RenderAction,只是直接输入 html,但我不断收到此错误消息:

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

Category 和 Home 控制器中的 Index 操作是直接返回 View 的 ActionResults。

有什么想法吗?

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    RenderSection 的语法错误。试试这个:

    @if (IsSectionDefined("Navigation"))
    {
        @RenderSection("Navigation", false)
    }
    else
    {
        <p>No navigation setup!</p>
    }
    

    【讨论】:

      【解决方案2】:

      我写了一篇博客文章,其中包含用于在 RenderSection 调用中指定默认内容的辅助方法:http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx

      它可以让你做以下事情:

      @RenderSection("Navigation", @<p>No navigation setup!</p>)
      

      【讨论】:

      • 哦,那是一个整洁的!谢谢:)
      猜你喜欢
      • 2023-01-14
      • 2020-12-07
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多