asp.net-mvc-3 – ASP.Net MVC 3剃刀:部分定义但未呈现错误
发布时间:2020-12-15 18:54:12 所属栏目:asp.Net 来源:网络整理
导读:我有以下布局模板: 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="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> 如果View.ShowLeftCol或View.ShowRightCol设置为false,我得到以下错误: 以下部分已定义,但尚未呈现为布局页“?/ Views / Shared / _Layout.cshtml”:“RightCol”。 我试图有一个单一的布局模板,而不是试图在运行时动态选择一个模板。有没有办法忽略此错误并继续呈现?任何人都可以想到另一种方式实现,这将允许我动态显示/隐藏列与Razor? 谢谢! 解决方法
在
ASP.net论坛上工作的建议。
基本上,如果我定义@section LeftCol在我的视图模板,但不运行任何调用RenderSection在我的布局中的代码,我得到错误,因为它不会被调用时View.ShowLeftCol为false。建议添加一个else块,并且基本上扔掉LeftCol部分中的任何内容。 @if (View.ShowLeftCol) { <div id="leftCol"> @RenderSection("LeftCol",false) </div> } else { WriteTo(new StringWriter(),RenderSection("LeftCol",false)); } 基于对记忆的关注,我决定测试以下。事实上,它也工作。 @if (showLeft) { <section id="leftcol"> <div class="pad"> @RenderSection("LeftColumn",false) </div> </section> } else { WriteTo(TextWriter.Null,RenderSection("LeftColumn",false)); } 另外,在我的页面的顶部,这是我的新逻辑为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; } 有人说它不为他们工作,但它的工作方式像我一样的魅力。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在SAAS体系结构中,如何处理多租户的数据库模式和
- asp.net – 存储库模式最佳实践
- asp.net – 将错误从HttpModule传递到MVC应用程序
- asp.net-mvc-3 – 尝试加载64位C/C++LI程序集时出现ASP.NET
- asp.net – 无法找到类型或命名空间名称’ProfileCommon
- asp.net – 在网站上放置广告的最佳做法?
- asp.net – IIS7 HttpModule和ISAPI Filter执行顺序
- asp.net signalR 专题—— 第三篇 如何从外部线程访问 Pers
- asp.net-mvc – 如何在ASP.NET MVC视图中使用扩展方法?
- asp.net-mvc-2 – MVC2 – > MVC3升级
推荐文章
站长推荐
- asp.net – SecurityException – 共享主机上的D
- asp.net – GetGlobalResourceObject或Resources
- azure – 获取#error = unsupported_response_ty
- asp.net – .NET Developer的Vagrant文??件(具有
- ASP.NET项目大小
- 你可以在ASP.NET中指定差异./,../,~/
- 如何保护我的ASP.NET AJAX应用程序?
- asp.net-mvc – 请求已中止:无法创建SSL / TLS安
- asp.net – IIS 8中的URL中缺少.aspx
- asp.net-mvc – 使用ASP.NET MVC剃刀的自定义控件
热点阅读