MVC @RenderSection"部分已经被定义,但尚未呈现"脚本。当页面的多个级别 [英] MVC @RenderSection "sections have been defined but have not been rendered" scripts. When multiple levels of page
问题描述
我与MVC V4的工作。结果
我有一个'_BootstrapLayout网页,其中定义了所有Twitter的引导等等的东西,它定义了网站的布局,导航栏等一个主页和网站页面从主页继承。
_BootstrapLayout.cshtml
_MainPage.cshtml @ {布局=〜/查看/共享/ _BootstrapLayout.cshtml }
Index.cshtml @ {布局=〜/查看/共享/ _MainPage.cshtml;}
块引用>所以,
母版页 - >主页 - >网站页面(S)该_BootstrapLayout页面包含脚本rendersection
@RenderSection(脚本,必需:false)
我想一个脚本部分添加到页面Index.cshtml,但是当我做我得到的异常
@section脚本{
<脚本类型=文/ JavaScript的> $(文件)。就绪(函数(){
... }); < / SCRIPT>
}
下面的部分已经被定义,但尚未呈现
。
对于布局页面〜/查看/共享/ _MainPage.cshtml:脚本
块引用>所以我添加一个空@section脚本的_MainPage.cshtml,还是一样的问题?
即使我添加code到_MainPage脚本的一节我仍然得到同样的错误。没关系,我把@section在_MainPage,仍然得到同样的错误。如果我故意不关闭的部分(即删除})然后我得到这预示着部分不正确的错误,所以它的解析中_MainPage部分。
我怎样才能获得@RenderSections的网站页面上的脚本,在这种情况下工作?
解决方案您需要重新定义部分
_MainPage.cshtml
。_BootstrapLayout.cshtml
@RenderSection(脚本,FALSE)
_MainPage.cshtml
@section脚本
{
@RenderSection(脚本,FALSE)
}Index.cshtml
@section脚本
{
<脚本>
//等等。
< / SCRIPT>
}I am working with MVC v4.
I have a '_BootstrapLayout' page which defines all the twitter bootstrap etc stuff, A main page which defines the site layout, navbar etc, and site pages which inherit from main page._BootstrapLayout.cshtml
_MainPage.cshtml @{ Layout = "~/Views/Shared/_BootstrapLayout.cshtml"; }
Index.cshtml @{Layout = "~/Views/Shared/_MainPage.cshtml";}
So Master Page --> Main Page --> site Page(s)
The _BootstrapLayout page contains a rendersection for scripts
@RenderSection("scripts", required: false)
I want to add a scripts section to the Index.cshtml page, but when I do I get the exception
@section scripts { <script type="text/javascript"> $(document).ready(function () { ... }); </script> }
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_MainPage.cshtml": "scripts".
So I added an empty @section scripts to the _MainPage.cshtml, still the same problem ? Even if I add code to the _MainPage scripts section I still get the same error. It doesn't matter where I put the @section in _MainPage, still get the same error.
If I purposely don't close the section (ie, delete the }) then I get an error which indicates that the section is incorrect, so it's parsing the section in _MainPage.
How can I get @RenderSections for scripts on the site pages to work in this case ?
解决方案You'll need to redefine the section in
_MainPage.cshtml
._BootstrapLayout.cshtml
@RenderSection("scripts", false)
_MainPage.cshtml
@section scripts { @RenderSection("scripts", false) }
Index.cshtml
@section scripts { <script> // etc. </script> }
这篇关于MVC @RenderSection&QUOT;部分已经被定义,但尚未呈现&QUOT;脚本。当页面的多个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!