ASP.Net MVC 3 Razor:部分已定义但未呈现错误 [英] ASP.Net MVC 3 Razor: Section Defined But Not Rendered Error

查看:26
本文介绍了ASP.Net MVC 3 Razor:部分已定义但未呈现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局模板:

<div id="mainColWrap"><div id="mainCol">@RenderBody()

@if (View.ShowLeftCol){<div id="leftCol">@RenderSection("LeftCol", required: false)

}@if (View.ShowRightCol){<div id="rightCol">@RenderSection("RightCol",需要:false)

}

如果 View.ShowLeftCol 或 View.ShowRightCol 设置为 false,则会出现以下错误:

<小时>

以下部分已定义但尚未为布局页面~/Views/Shared/_Layout.cshtml"呈现:RightCol".

<小时>

我正在尝试使用单个布局模板,而不是尝试在运行时动态选择模板.有没有办法忽略这个错误并继续渲染?谁能想到另一种实现方式,让我可以使用 Razor 动态显示/隐藏列?

谢谢!

解决方案

ASP.net

上得到了建议a> 有效的论坛.

本质上,如果我在我的视图模板中定义了 @section LeftCol 但没有在我的布局中运行任何调用 RenderSection 的代码,我会收到错误,因为当 View.ShowLeftCol 为 false 时它不会被调用.建议是添加一个 else 块并基本上丢弃 LeftCol 部分中的任何内容.

@if (View.ShowLeftCol){<div id="leftCol">@RenderSection("LeftCol", false)

}别的{WriteTo(new StringWriter(), RenderSection("LeftCol", false));}

<小时>

基于对内存的担忧,我决定也测试以下内容.事实上它也有效.

@if (showLeft){<section id="leftcol"><div class="pad">@RenderSection("LeftColumn", false)

</节>}别的{WriteTo(TextWriter.Null, RenderSection("LeftColumn", false));}

此外,在我的页面顶部,这是我对 showLeft/showRight 的新逻辑:

bool showLeft = IsSectionDefined("LeftColumn");bool showRight = IsSectionDefined("RightColumn");布尔?hideLeft = (bool?)ViewBag.HideLeft;布尔?hideRight = (bool?)ViewBag.HideRight;if (hideLeft.HasValue && hideLeft.Value == true) { showLeft = false;}if (hideRight.HasValue && hideRight.Value == true) { showRight = false;}

有人说这对他们不起作用,但对我来说却是一种魅力.

I have the following layout template:

<div id="columns" class="@View.LayoutClass">
    <div id="mainColWrap">
        <div id="mainCol">
            @RenderBody()
        </div>
    </div>
    @if (View.ShowLeftCol){
    <div id="leftCol">
        @RenderSection("LeftCol", required: false)
    </div>
    }
    @if (View.ShowRightCol){
    <div id="rightCol">
        @RenderSection("RightCol", required: false)
    </div>
    }
</div>

If View.ShowLeftCol or View.ShowRightCol are set to false, I get the following error:


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


I am trying to have a single layout template instead of trying to dynamically select a template at runtime. Is there a way to ignore this error and continue rendering? Can anyone think of another way to implement that would allow me to dynamically show/hide columns with Razor?

Thanks!

解决方案

Was given a suggestion on the ASP.net forums that works.

Essentially, if I define @section LeftCol in my view template but don't run any code that calls RenderSection in my layout, I get the error because it doesn't get called when View.ShowLeftCol is false. The suggestion was to add an else block and essentially throw away whatever contents are in the LeftCol section.

@if (View.ShowLeftCol)
{ 
<div id="leftCol"> 
    @RenderSection("LeftCol", false) 
</div> 
}
else
{
    WriteTo(new StringWriter(), RenderSection("LeftCol", false));
}


Based on the concern raised about memory I decided to test the following out as well. Indeed it also works.

@if (showLeft)
{
    <section id="leftcol">
        <div class="pad">
            @RenderSection("LeftColumn", false)
        </div>
    </section>
}
else
{
    WriteTo(TextWriter.Null, RenderSection("LeftColumn", false));
}

Also, at the top of my page, this is my new logic for showLeft/showRight:

bool showLeft = IsSectionDefined("LeftColumn");
bool showRight = IsSectionDefined("RightColumn");
bool? hideLeft  = (bool?)ViewBag.HideLeft;
bool? hideRight = (bool?)ViewBag.HideRight;
if (hideLeft.HasValue && hideLeft.Value == true) { showLeft = false; }
if (hideRight.HasValue && hideRight.Value == true) { showRight = false; }

Someone else said it didn't work for them, but it worked like a charm for me.

这篇关于ASP.Net MVC 3 Razor:部分已定义但未呈现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆